August 2012
Intermediate to advanced
976 pages
30h 17m
English
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
Read now
Unlock full access