I see a lot of c++ code that looks like this:
for( const_iterator it = list.begin(),
const_iterator ite = list.end();
it != ite; ++it)
<
The two versions are not the same though. In the second version it compares the iterator against list.end()
every time, and what list.end()
evaluates to might change over the course of the loop. Now of course, you cannot modify list
through the const_iterator it
; but nothing prevents code inside the loop from just calling methods on list
directly and mutating it, which could (depending on what kind of data structure list
is) change the end iterator. It might therefore be incorrect in some circumstances to store the end iterator beforehand, because that may no longer be the correct end iterator by the time you get to it.