In addition to the types listed in Table 9.2 (p. 330), the associative containers define the types listed in Table 11.3. These types represent the container’s key and value types.
For the set
types, the key_type
and the value_type
are the same; the values held in a set
are the keys. In a map
, the elements are key–value pairs. That is, each element is a pair
object containing a key and a associated value. Because we cannot change an element’s key, the key part of these pair
s is const
:
set<string>::value_type v1; // v1 is a string
set<string>::key_type v2; // v2 is a string
map<string, int>::value_type v3; // v3 is a pair<const string, int>
map<string, int>::key_type v4; // v4 is a string
map<string, int>::mapped_type v5; // v5 is an int
As with the sequential containers (§ 9.2.2, p. 332), we use the scope operator to fetch a type member—for example, map<string, int>::key_type
.
Only the map
types (unordered_map
, unordered_multimap
, multimap
, and map
) define mapped_type
.