Noah Roberts wrote:
Markus Schoder wrote:
>>
list erase may only invalidate iterators to the erased element.
I doubt it.
23.2.2.3 [lib.list.modifiers]
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
Effects: Invalidates only the iterators and references to the
erased elements.
Note - "the iterators" here refers to 'position', 'first', and 'last',
not to all iterators. Compare with the wording for
[lib.deque.modifiers] which states that _all_ iterators and
pointers to the deque are invalidated.
Think about what happens:
iterator it = v.begin() + 3;
operator+ is not defined for std::list.
But you can achieve the same effect by incrementing 3 times.
v.erase(v.begin());
What does it point to now?
The same item it did before.
>
>Iter should still be valid. As has been pointed out the code can be
improved but I see no reason why the given code has undefined behavior.
What happens when begin() + 1 == end()?
iterator it = begin();
temp = *it; // ok
iterator tempit = it;
++it; // it == end().
Note: it == end().
v.erase(tempit); // begin() == end().
delete temp;
end state:
v is empty.
Yes
it points to begin() + 1.
Actually, it == end(), as noted above.
The expression "begin() + 1" is meaningless, as operator+ is
not defined for lists. I guess you mean 'the element after begin()',
but that isn't where it is pointing.
Continue the loop...
If your loop now increments 'it', then it should be considered
a bug in the loop code.