title: std::multimap and equal_range
body: <p>I'm having some headaches with the <code>std::multimap</code> container and I wish to know what would be the proper way to achieve my goal. Basically, here is my <a href="http://sscce.org/" rel="nofollow noreferrer">SSCCE</a>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  
  int main ()
  {
      typedef std::multimap&lt;int, int&gt; CollectionType;
      typedef std::pair&lt;CollectionType::iterator, CollectionType::iterator&gt; RangeType;
  
      CollectionType multiMap;
  
      multiMap.insert(std::make_pair(1, 1));
      multiMap.insert(std::make_pair(1, 2));
      multiMap.insert(std::make_pair(2, 3));
      multiMap.insert(std::make_pair(2, 4));
      multiMap.insert(std::make_pair(2, 5));
      multiMap.insert(std::make_pair(3, 1));
  
      RangeType range = multiMap.equal_range(2);
      for (CollectionType::iterator iterator = range.first; iterator != range.second; ++iterator)
      {
          if (iterator-&gt;second == 4)
          {
              //multiMap.erase(iterator);//ISSUE 1
          }
          else
          {
              //iterator-&gt;first = -1;//ISSUE 2
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>As you can see above, I need to select a range of the <code>multimap</code> for a given key and then:</p>
  
  <ol>
  <li>I need to erase certain rows from the range</li>
  <li>I need to change the key of other rows from the range</li>
  </ol>
  
  <p>Regarding 1, since "<a href="http://en.cppreference.com/w/cpp/container/multimap/erase" rel="nofollow noreferrer">references and iterators to the erased elements are invalidated</a>", how can I remove those elements? Should I push the specific iterators in some container and iterate over it once the loop is over? I saw <a href="https://stackoverflow.com/a/446295/1174378">this answer</a>, but it seems a bit hackish / ugly / error prone / etc...</p>
  
  <p>Regarding 2, since the "dumb" approach won't (obviously) work, what would be a good approach to achieve what I need? I could remove the elements and insert new ones in their place, once I solve issue 1, but wouldn't that perhaps mess up the iteration if, let's say I mistakenly insert an item with the same key as the one I just removed?...</p>
  
  <pre><code>int second = iterator-&gt;second;
  localEdges.smarter_erase(iterator);
  localEdges.insert(std::make_pair(-1, second));
  </code></pre>