C++ 中的 set 和 map 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22088607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between set vs map in C++?
提问by SheikCode
I am still confused by the differences between the map and set datastructures in STL. I know set is storing the values in a sorted way, what about map? Does it store the values in sorted order? Map stores pairs of values (key,value), what is the advantage of this feature?
我仍然对 STL 中 map 和 set 数据结构之间的差异感到困惑。我知道 set 以排序的方式存储值,那么 map 呢?它是否按排序顺序存储值?Map 存储成对的值(键,值),这个特性有什么好处?
回答by Vijay Rao
At least for the ordered versions (std::map
and std::set
), a map
facilitates use-cases of a set
by allowing you to introduce an external key (map::key_type
) to determine ordering of the elements that otherwise can't be derived from map
's data type (map::mapped_type
). If the ordering can be wholly derived (by comparing 2 elements) from map::mapped_type
, then you're typically better off using a set
, in which case you'll avoid duplicating the key as map::key_type
.
至少对于有序版本 (std::map
和std::set
), a通过允许您引入外部键 ( ) 来确定无法从的数据类型 ( )派生的元素的排序,从而map
简化了 a 的用例。如果可以从 完全派生(通过比较 2 个元素)排序,那么您通常最好使用 a ,在这种情况下,您将避免将密钥复制为。set
map::key_type
map
map::mapped_type
map::mapped_type
set
map::key_type
In a way, std::map
is redundant and you can always use std::set
instead by introducing a new element type which aggregates keys with data while providing the necessary comparison function. However, this is cumbersome and typically inelegant.
在某种程度上,它std::map
是多余的,您始终可以std::set
通过引入一种新的元素类型来代替使用,该类型将键与数据聚合在一起,同时提供必要的比较功能。然而,这很麻烦并且通常不优雅。
To clarify why a set
may be cumbersome over a map
; A set
will store the <key, data>
pair as an element while map
will maintain a separation between the 2. This means, for instance, that for a find
operation on a set
where find
's parameter is constructed on-the-spot, an entire <key, data>
element will have to be constructed while it's really on the key
that's needed for the find
operation. The construction of the data
members of a set
's element is then redundant, and can be rather inefficient if, for instance, data
members represent disk storage or involve some other complex or else time consuming construction operation. map
alleviates this by only having to construct the actual key
required for find
.
澄清为什么 aset
可能比 a 麻烦map
;Aset
会将这<key, data>
对存储为一个元素,同时map
将保持 2 之间的分离。这意味着,例如,对于现场构造where参数的find
操作,必须同时构造整个元素这确实是操作所需的。因此, a元素的成员的构造是多余的,并且如果,例如,成员代表磁盘存储或涉及一些其他复杂或耗时的构造操作,则效率可能相当低。只需构建实际所需的set
find
<key, data>
key
find
data
set
data
map
key
find
.
To summarize, consider an element <key, data>
for which you're wondering whether to use a map
or a set
to store multiple ordered elements. If key
spans the entire data
(meaning data
is empty or else key == data
) then you're better off using a set
in which case you'll avoid a) duplicating key
storage and b) likely having to keep 2 key
s synchronized. If key
is not contained in data
then (you have to) use a map
. The tricky part is when key
is a (proper) subset of data
. You then have to trade-off the cost of maintaining duplicate key
s (for a map
) vs the cost of constructing data
that doesn't overlap with key
(for a set
), the latter which may occur for find
operations.
总而言之,考虑一个<key, data>
您想知道是使用 amap
还是 aset
来存储多个有序元素的元素。如果key
跨越整个data
(意思data
是空或否则key == data
),那么你最好使用 aset
在这种情况下,你将避免 a) 重复key
存储和 b) 可能必须保持 2 key
s 同步。如果key
不包含在data
then (您必须)中,请使用map
. 棘手的部分是 whenkey
是data
. 然后,您必须权衡维护重复key
s(对于 a map
)的成本与data
不与key
(对于 a set
)重叠的构建成本 ,后者可能发生在find
操作。
回答by NPE
回答by Alexey Voytenko
A map
stores keys sorted. It maps keys to values. Usually it is implemented as a binary search tree (red-black tree) for keys. A set
is a map where values are irrelevant.
unordered_map
and unordered_set
(new in C++11) store keys unsorted and use hash table for search.
Amap
存储已排序的键。它将键映射到值。通常它被实现为键的二叉搜索树(红黑树)。Aset
是值不相关的映射。
unordered_map
和unordered_set
(C++11 中的新功能)存储未排序的键并使用哈希表进行搜索。
回答by SzG
std::map
and std::set
are extremely similar. They both have a sorted collection of unique keys. Additionally, map
has a value associated with each key.
std::map
并且std::set
极其相似。它们都有一个排序的唯一键集合。此外,map
具有与每个键关联的值。