Chapter 8. Using set and map

Difficulty: 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]

    1. 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;
      }
      
    2. 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 ) );
      }
      
  1. Can you modify a set's contents through a set::iterator? Why or why not?

Solution

Associative Containers: Review

First, a quick review: Here are some key points about what it means ...

Get More Exceptional C++ 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.