title: Erase by iterator on a C++ STL map
body: <p>I'm curious about the rationale behind the following code. For a given map, I can delete a range up to, but not including, <code>end()</code> (obviously,) using the following code:</p>
  
  <pre><code>map&lt;string, int&gt; myMap;
  myMap["one"] = 1;
  myMap["two"] = 2;
  myMap["three"] = 3;
  
  map&lt;string, int&gt;::iterator it = myMap.find("two");
  
  myMap.erase( it, myMap.end() );
  </code></pre>
  
  <p>This erases the last two items using the range. However, if I used the single iterator version of erase, I half expected passing <code>myMap.end()</code> to result in no action as the iterator was clearly at the end of the collection. This is as distinct from a corrupt or invalid iterator which would clearly lead to undefined behaviour.</p>
  
  <p>However, when I do this:</p>
  
  <pre><code>myMap.erase( myMap.end() );
  </code></pre>
  
  <p>I simply get a segmentation fault. I wouldn't have thought it difficult for map to check whether the iterator equalled <code>end()</code> and not take action in that case. Is there some subtle reason for this that I'm missing? I noticed that even this works:</p>
  
  <pre><code>myMap.erase( myMap.end(), myMap.end() );
  </code></pre>
  
  <p>(i.e. does nothing)</p>
  
  <p>The reason I ask is that I have some code which receives a valid iterator to the collection (but which could be <code>end()</code>) and I wanted to simply pass this into erase rather than having to check first like this:</p>
  
  <pre><code>if ( it != myMap.end() )
      myMap.erase( it );
  </code></pre>
  
  <p>which seems a bit clunky to me. The alternative is to re code so I can use the by-key-type erase overload but I'd rather not re-write too much if I can help it.</p>