A map is a collection of a keyvalue pairs , such as an address and phone number keyed to an individual's name. In contrast, a set is simply a collection of keys . For example, a business might define a set named bad_checks, to hold the names of individuals who have issued bad checks to the company. A set is most useful when we simply want to know whether a value is present . Before accepting a check, for example, that business would query bad_checks to see whether the customer’s name was present.
[Note] With two exceptions , set supports the same operations as map. The exceptions are that set does not provide a subscript operator and does not define mapped_type . In a set, the value_type is not a pair: instead it and key_type are the same type. They are each the type of the elements stored in the set. These differences reflect the fact that set holds only key; there is no value associated with the key . As with map, the keys of a set must be unique and may not be changed .
To use a set, we must include the set header . The operations on sets are essentially identical to those on maps.
[An example: to use set]
output:
[Another example]
[set used]
[map used]
本文介绍了集合(set)和映射(map)两种数据结构的基本概念及应用案例。解释了两者之间的主要区别,包括它们如何存储和处理数据。并通过示例代码展示了如何使用C++中的set和map来实现特定的功能。
2424

被折叠的 条评论
为什么被折叠?



