multimap相对于矢量图的优势是什么? [英] What's the advantage of multimap over map of vectors?
问题描述
我不明白为什么multimap存在,如果我们可以创建矢量或集合地图。
对我来说,区别只是:
I don't understand why multimap exists if we can create map of vectors or map of sets. For me only differences are:
- 在multimap中使用
equal_range
获取键的元素和向量的映射,我们只需使用[]
运算符并具有元素的向量。 - 使用
multimap.insert(make_pair(key,value))
在多图中添加元素和map_of_vectors [key] .push_back(value)
- 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. - using
multimap.insert(make_pair(key,value))
in multimap for adding elements andmap_of_vectors[key].push_back(value)
in map of vectors.
那么为什么要使用multimap?对我来说,最好有一个向量而不是两个迭代器来获取所有的键值。
So why use multimap? For me it's better to have a vector than two iterators to get all values of a key.
这个问题也适用于unordered_map的向量和unordered_multimap。 b $ b
This question applies also to unordered_map of vectors and unordered_multimap.
推荐答案
我会说这取决于具有相同键值的所有值是否具有您要解决的关系。
I would say it depends on whether all the values with same key have a relationship that you want to address.
例如,你经常使用键X来遍历所有元素,或者将它们传递给一个函数,等等?
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.
然而,如果你只有一个项目的集合,可以共享同一个键值,或为什么在两者之间使用向量?使用迭代器运行multimap比为地图,向量case使用嵌套的for循环更方便。
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.
另一种看法是:如果多个条目键是很常见的,你的结构在地图中更有效率,向量case。如果他们很少发生,则是相反的。
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.
这篇关于multimap相对于矢量图的优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!