In Item 7, we considered vector and deque . This time, the focus is on more news about the associative containers set, multiset, map , and multimap . [14]
[14] Note that set and multiset aren't really "associative" in the usual sense of relating a lookup key with another value, the way a dictionary does. That's the job of map and multimap . But all four of these containers appear in the C++ standard under the heading "Associative Contain-ers," so I'll follow suit to be consistent.
What's wrong with the following code? How would you correct it?
map<int,string>::iterator i = m.find( 13 ); if( i != m.end() ) { const_cast<int&>( i->first ) = 9999999; }
To what extent are the problems fixed by writing the following instead?
map<int,string>::iterator i = m.find( 13 ); if( i != m.end() ) { string s = i->second; m.erase( i ); m.insert( make_pair( 9999999, s ) ); }
Can you modify a set 's contents through a set::iterator ? Why or why not?