title: vectors: rend() is being invalidated by erase()
body: <p>According to the C++ specification (23.2.4.3), vector::erase() only invalidates "all the iterators and references after the point of the erase"</p>
  
  <p>As such, when using reverse_iterators to pass over all vector members, an erase on the current iterator should <em>not</em> cause the rend() member to be invalidated.</p>
  
  <p>This code will run under G++ but will provide a runtime exception on Windows (VS2010):</p>
  
  <pre><code>#include &lt;vector&gt;
  
  using namespace std;
  
  int main()
  {
      vector&lt;int&gt; x;
      x.push_back(1);
      x.push_back(2);
      x.push_back(3);
  
      //Print
      for(vector&lt;int&gt;::const_iterator i = x.begin(); i != x.end(); ++i)
          printf("%d\n", *i);
  
      //Delete second node
      for(vector&lt;int&gt;::reverse_iterator r = x.rbegin(); r != x.rend(); ++r)
          if(*r == 2)
              x.erase((r+1).base());
  
      //Print
      for(vector&lt;int&gt;::const_iterator i = x.begin(); i != x.end(); ++i)
          printf("%d\n", *i);
  
      return 0;
  }
  </code></pre>
  
  <p>The error is interesting:</p>
  
  <blockquote>
    <p>Expression: vector iterator not decrementable</p>
  </blockquote>
  
  <p>Given on the line of the second for loop upon the second run. The decrement refers to the internal "current" iterator member of the reverse_iterator, which is decremented whenever reverse_iterator is incremented.</p>
  
  <p>Can anyone explain this behavior, please? </p>
  
  <p>Thanks.</p>
  
  <p>EDIT</p>
  
  <p>I think this code sample better shows that it's not a problem with r, but rather with rend():</p>
  
  <pre><code>//Delete second node
  for(vector&lt;int&gt;::reverse_iterator r = x.rbegin(); r != x.rend();)
  {
      vector&lt;int&gt;::reverse_iterator temp = r++;
  
      if(*temp == 2)
          x.erase((temp+1).base());
  }
  </code></pre>
  
  <p>And errors out with <code>vector iterators incompatible</code> on the for loop upon entry after erase.</p>