I don't understand why multimap exists if we can create map of vectors or map of sets. For me only differences are:
如果我们可以创建矢量图或集合图,我不明白为什么存在多图。对我来说,唯一的区别是:
- using
equal_range
in multimap for getting elements of a key and in map of vectors we simply use[]
operator and have vector of elements. - 使用multimap中的equal_range来获取键的元素,并在向量的映射中,我们只需使用[]运算符并具有元素的向量。
- using
multimap.insert(make_pair(key,value))
in multimap for adding elements andmap_of_vectors[key].push_back(value)
in map of vectors. - 在multimap中使用multimap.insert(make_pair(key,value))在向量的映射中添加元素和map_of_vectors [key] .push_back(value)。
So why use multimap? For me it's better to have a vector than two iterators to get all values of a key.
那么为什么要使用multimap呢?对我来说,最好有一个向量而不是两个迭代器来获取一个键的所有值。
This question applies also to unordered_map of vectors and unordered_multimap.
此问题也适用于vector和unordered_multimap的unordered_map。
3 个解决方案
#1
43
I would say it depends on whether all the values with same key have a relationship that you want to address.
我会说这取决于具有相同键的所有值是否具有您想要解决的关系。
So for example, do you often go through all elements with key X, or pass them to a function, and so on? Then it is more convenient to already have them in their separate container, that you can address directly.
例如,您是否经常使用键X遍历所有元素,或者将它们传递给函数,依此类推?然后将它们放在单独的容器中更方便,您可以直接进行寻址。
However, if you just have a collection of items, that may share same key value, or not, why use vectors in between? It is more convenient to run through the multimap with iterators than have a nested for loop for the map, vector case.
但是,如果您只有一个项目集合,可能共享相同的键值,为什么要在它们之间使用向量?使用迭代器运行multimap比使用map for vector case的嵌套for循环更方便。
Another way of looking at this: If multiple entries per key is very common, your structure is more efficient in the map, vector case. If they seldomly happen, it is the opposite.
另一种看待这种情况的方法:如果每个键的多个条目非常常见,那么您的结构在地图中的效率会更高。如果他们很少发生,则相反。
#2
40
There are many important differences between multimap<x, y>
and map<x, vector<y>>
multimap
Once you had inserted a value into multimap, you know that the iterator would remain valid until you remove it and this is very strong property, you can't have it with map of vectors.
一旦你在multimap中插入了一个值,你知道迭代器在你删除它之前一直有效,这是一个非常强大的属性,你不能用它来映射向量。
multimap<x,y>::iterator p=mymap.insert(make_pair(a,b));
The iterator remains valid until it is erased from map, while in second case, it would be invalidated each time you add new entry to the vector.
迭代器在从映射中删除之前一直有效,而在第二种情况下,每次向向量添加新条目时它都会失效。
Also note that map<x, vector<y>>
may have an empty value set with existing key, while multimap does not.
另请注意,map
These are different things that behave differently.
这些是表现不同的不同事物。
And to be honest I miss multimap in some languages that do not provide it in their library.
说实话,我想念一些语言中的multimap,而这些语言并没有在他们的库中提供。
#3
-1
two iterators ??? I think you are wrong.
两个迭代器???我认为你错了。
when i use std::for_each() or other algo of on a multimap I use only ONE iterator range, and it is damn much simpler that worrying with a vector for each key.
当我在多图上使用std :: for_each()或其他算法时,我只使用一个迭代器范围,而且每个键的向量都令人担心,这简直太简单了。
#1
43
I would say it depends on whether all the values with same key have a relationship that you want to address.
我会说这取决于具有相同键的所有值是否具有您想要解决的关系。
So for example, do you often go through all elements with key X, or pass them to a function, and so on? Then it is more convenient to already have them in their separate container, that you can address directly.
例如,您是否经常使用键X遍历所有元素,或者将它们传递给函数,依此类推?然后将它们放在单独的容器中更方便,您可以直接进行寻址。
However, if you just have a collection of items, that may share same key value, or not, why use vectors in between? It is more convenient to run through the multimap with iterators than have a nested for loop for the map, vector case.
但是,如果您只有一个项目集合,可能共享相同的键值,为什么要在它们之间使用向量?使用迭代器运行multimap比使用map for vector case的嵌套for循环更方便。
Another way of looking at this: If multiple entries per key is very common, your structure is more efficient in the map, vector case. If they seldomly happen, it is the opposite.
另一种看待这种情况的方法:如果每个键的多个条目非常常见,那么您的结构在地图中的效率会更高。如果他们很少发生,则相反。
#2
40
There are many important differences between multimap<x, y>
and map<x, vector<y>>
multimap
Once you had inserted a value into multimap, you know that the iterator would remain valid until you remove it and this is very strong property, you can't have it with map of vectors.
一旦你在multimap中插入了一个值,你知道迭代器在你删除它之前一直有效,这是一个非常强大的属性,你不能用它来映射向量。
multimap<x,y>::iterator p=mymap.insert(make_pair(a,b));
The iterator remains valid until it is erased from map, while in second case, it would be invalidated each time you add new entry to the vector.
迭代器在从映射中删除之前一直有效,而在第二种情况下,每次向向量添加新条目时它都会失效。
Also note that map<x, vector<y>>
may have an empty value set with existing key, while multimap does not.
另请注意,map
These are different things that behave differently.
这些是表现不同的不同事物。
And to be honest I miss multimap in some languages that do not provide it in their library.
说实话,我想念一些语言中的multimap,而这些语言并没有在他们的库中提供。
#3
-1
two iterators ??? I think you are wrong.
两个迭代器???我认为你错了。
when i use std::for_each() or other algo of on a multimap I use only ONE iterator range, and it is damn much simpler that worrying with a vector for each key.
当我在多图上使用std :: for_each()或其他算法时,我只使用一个迭代器范围,而且每个键的向量都令人担心,这简直太简单了。