title: Why is comparing against "end()" iterator legal?
body: <p>According to C++ standard (3.7.3.2/4) <em>using</em> (not only dereferencing, but also copying, casting, whatever else) an invalid pointer is undefined behavior (in case of doubt also see <a href="https://stackoverflow.com/questions/1866461/why-should-i-not-try-to-use-this-value-after-delete-this">this question</a>). Now the typical code to traverse an STL containter looks like this:</p>
  
  <pre><code>std::vector&lt;int&gt; toTraverse;
  //populate the vector
  for( std::vector&lt;int&gt;::iterator it = toTraverse.begin(); it != toTraverse.end(); ++it ) {
      //process( *it );
  }
  </code></pre>
  
  <p><code>std::vector::end()</code> is an iterator onto the hypothetic element <em>beyond</em> the last element of the containter. There's no element there, therefore using a pointer through that iterator is undefined behavior.</p>
  
  <p>Now how does the <code>!= end()</code> work then? I mean in order to do the comparison an iterator needs to be constructed wrapping an invalid address and then that invalid address will have to be used in a comparison which again is undefined behavior. Is such comparison legal and why?</p>