Search code examples
c++stdreverse-iterator

Erasing the last element with the reverse_iterator


Recently I found the following in the code I'm maintaining:

for (reverse_iterator rit = base_container::rbegin(); rit != base_container::rend() && 0 < N; N--) 
{
  another_container->push_back(*rit);
  base_container::erase((++rit).base());
}

It tries to remove the last element from a container (std::list in this case) in a loop using the reverse_iterator. The point is that it looks like it should work properly, but it doesn't (some memory corruptions occur due to invalid iterators) and I wonder why? Is there any restriction or rule to not do so?

Thanks.

P.S. To prevent any improvements for the solution, I've already re-written it to make it work. The question is why the code above is not working properly?


Solution

  • All iterators after calling erase are invalidated. However, erase returns an iterator that you can use.