在C ++中set vs map有什么区别? [英] What is the difference between set vs map in C++?

查看:136
本文介绍了在C ++中set vs map有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然困惑的地图和集数据结构在STL之间的差异。我知道集是存储的值在一个排序的方式,地图?它是否按照排序顺序存储值?
映射存储值对(键,值),这个特性的优点是什么?

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?

推荐答案

对于有序版本( std :: map std :: set ),映射通过允许您引入外部键( map :: key_type )来帮助使用 set $ c>)来确定不能从 map 的数据类型( map :: data_type )。如果可以从 map :: data_type 完全导出排序(通过比较2个元素),那么通常最好使用 set ,在这种情况下,您将避免重复键 map :: key_type

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::data_type). If the ordering can be wholly derived (by comparing 2 elements) from map::data_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 元素类型,其在提供必要的比较功能的同时将密钥与数据聚合。

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.

为了说明为什么设置可能比<$ c更麻烦$ c> map ; A set 将存储< key,data> 对作为元素,而 map 将保持2之间的分隔。这意味着,例如,对找到操作在设置其中 find 的参数是现场构造的,整个< key,data> 元素将被构造,而它真正在,这是 find 操作所需的。一个 set 数据成员的构造是多余的,并且如果,实例, data 成员表示磁盘存储或涉及一些其他复杂或耗时的构建操作。 map 只需要构建所需的实际 $ c>。

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.

总而言之,考虑一个元素< key,data> 是否使用映射设置来存储多个有序元素。如果跨整个数据(表示数据为空或者 key == data ),那么你最好使用 set )重复存储和b)可能必须保持2 同步。如果 key 不包含在 data 中,那么(必须)使用 code>。棘手的部分是数据的(正确)子集。您必须平衡维护重复(对于映射)的成本与构建成本键不重叠的数据设置),后者可能发生找到操作。

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 keys 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 keys (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.

这篇关于在C ++中set vs map有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆