December 2001
Intermediate to advanced
304 pages
6h 25m
English
set and mapDifficulty: 5
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]
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?
First, a quick review: Here are some key points about what it means ...
Read now
Unlock full access