简体   繁体   English

我可以使用C ++映射,其中多个键引用值而不使用指针吗?

[英]Can I have a C++ map where multiple keys reference the value without using pointers?

From a C background I find myself falling back into C habits where there is generally a better way. 从C背景中我发现自己回到了C习惯,通常有更好的方式。 In this case I can't think of a way to do this without pointers. 在这种情况下,我想不出没有指针的方法。

I would like 我想要

struct foo {
    int i;
    int j;
};

mymap['a'] = foo
mymap['b'] = bar

As long as only one key references a value mymap.find will return a reference so I can modify the value, but if I do this: 只要只有一个键引用一个值mymap.find将返回一个引用,所以我可以修改该值,但如果我这样做:

mymap['c'] = mymap.find('a') // problematic because foo is copied right?

The goal is to be able to find 'a' or 'c' modify foo and then the next find of 'a' or 'c' will show the updated result. 目标是能够找到'a'或'c'修改foo,然后下一个'a'或'c'的查找将显示更新的结果。

No, you will need to use pointers for this. 不,你需要使用指针。 Each entry in the map maintains a copy of the value assigned, which means that you cannot have two keys referring to the same element. 地图中的每个条目都保留所分配值的副本 ,这意味着您不能有两个引用相同元素的键。 Now if you store pointers to the element, then two keys will refer to two separate pointers that will refer to the exact same in memory element. 现在,如果存储指向元素的指针,那么两个键将引用两个单独的指针,这些指针将引用内存元素中完全相同的指针。

For some implementation details, std::map is implemented as a balanced tree where in each node contains a std::pair<const Key,Value> object (and extra information for the tree structure). 对于某些实现细节, std::map实现为平衡树,其中每个节点包含一个std::pair<const Key,Value>对象(以及树结构的额外信息)。 When you do m[ key ] the node containing the key is looked up or a new node is created in the tree and the reference to the Value subobject of the pair is returned. 当您执行m[ key ]将查找包含该键的节点,或者在树中创建一个新节点,并返回该对的Value子对象的引用。

I would use std::shared_ptr here. 我会在这里使用std::shared_ptr You have an example of shared ownership, and shared_ptr is made for that. 您有一个共享所有权的示例,并为此创建了shared_ptr While pointers tend to be overused, it is nothing wrong with using them when necessary. 虽然指针往往被过度使用,但在必要时使用它们并没有错。

Boost.Intrusive Boost.Intrusive

Boost.Intrusive is a library presenting some intrusive containers to the world of C++. Boost.Intrusive是一个向C ++世界提供一些侵入式容器的库。 Intrusive containers are special containers that offer better performance and exception safety guarantees than non-intrusive containers (like STL containers). 侵入式容器是特殊容器,与非侵入式容器(如STL容器)相比,可提供更好的性能和异常安全保证。

The performance benefits of intrusive containers makes them ideal as a building block to efficiently construct complex containers like multi-index containers or to design high performance code like memory allocation algorithms. 侵入式容器的性能优势使其成为有效构建复杂容器(如多索引容器)或设计高性能代码(如内存分配算法)的构建块。

While intrusive containers were and are widely used in C, they became more and more forgotten in C++ due to the presence of the standard containers which don't support intrusive techniques.Boost.Intrusive not only reintroduces this technique to C++, but also encapsulates the implementation in STL-like interfaces. 虽然侵入式容器在C中被广泛使用,但由于存在不支持侵入式技术的标准容器,它们在C ++中变得越来越被遗忘.Boost.Intrusive不仅将这种技术重新引入C ++,而且还封装了在类似STL的接口中实现。 Hence anyone familiar with standard containers can easily use Boost.Intrusive. 因此,任何熟悉标准容器的人都可以轻松使用Boost.Intrusive。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM