Is it allowed to increment an iterator variable it
that already is at end()
, i.e. auto it = v.end()
?
- Is it allowed in general?
- If not, is it not allowed for
vector
? - If yes, is
++it
maybe idempotent ifit==v.end()
?
I ask, because I stumbled upon code like this:
std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7 };
// delete every other element
for(auto it=v.begin(); it<v.end(); ++it) { // it<end ok? ++it ok on end?
it = v.erase(it);
}
It works fine with g++-6, but that is no proof.
For one it<v.end()
may only work with vectors
, I suppose it should read it!=v.end()
in general. But in this example that would not recognize the end of v
if ++it
is applied when it already is on the end.