title: What happens if you increment an iterator that is equal to the end iterator of an STL container
body: <p>What if I increment an iterator by 2 when it points onto the last element of a vector? In <a href="https://stackoverflow.com/questions/1057529/how-to-increment-an-iterator-by-2">this question</a> asking how to adjust the iterator to an STL container by 2 elements two different approaches are offered:</p>
  
  <ul>
  <li>either use a form of arithmetic operator - +=2 or ++ twice</li>
  <li>or use std::advance()</li>
  </ul>
  
  <p>I've tested both of them with VC++ 7 for the edge case when the iterator points onto the last element of the STL container or beyond:</p>
  
  <pre><code>vector&lt;int&gt; vec;
  vec.push_back( 1 );
  vec.push_back( 2 );
  
  vector&lt;int&gt;::iterator it = vec.begin();
  advance( it, 2 );
  bool isAtEnd = it == vec.end(); // true
  it++; // or advance( it, 1 ); - doesn't matter
  isAtEnd = it == vec.end(); //false
  it = vec.begin();
  advance( it, 3 );
  isAtEnd = it == vec.end(); // false
  </code></pre>
  
  <p>I've seen may times an advise to compare against vector::end() when traversing the vector and other containers:</p>
  
  <pre><code>for( vector&lt;int&gt;::iterator it = vec.begin(); it != vec.end(); it++ ) {
      //manipulate the element through the iterator here
  }
  </code></pre>
  
  <p>Obviously if the iterator is advanced past the last element inside the loop the comparison in the for-loop statement will evaluate to false and the loop will happily continue into undefined behaviour.</p>
  
  <p>Do I get it right that if I ever use advance() or any kind of increment operation on an iterator and make it point past the container's end I will be unable to detect this situation? If so, what is the best practice - not to use such advancements?</p>