11.3.1. Associative Container Iterators
When we dereference an iterator, we get a reference to a value of the container’s value_type
. In the case of map
, the value_type
is a pair
in which first
holds the const
key and second
holds the value:
// get an iterator to an element in word_countauto map_it = word_count.begin();// *map_it is a reference to a pair<const string, size_t> objectcout << map_it->first; // prints the key for this elementcout << " " << map_it->second; // prints the value of the elementmap_it->first = "new key"; // error: key is const++map_it->second; // ok: we can change the value through an iterator
Note
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.