Consider the toy program (post.cpp):
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int > a;
int i;
for(i=0;i<10;i++)
a.push_back(i);
auto it=a.rbegin();
while(it!=a.rend()) {
if ((*it % 2)==0) {
cout << "about to erase "<<*it<<endl;
a.erase((it++).base());
}
else {
++it;
}
}
for(auto it2=a.begin(); it2 != a.end(); it2++) {
cout << *it2 << endl;
}
return 0;
}
What I am trying to do is to test for evenness, and then delete the current number, since (it++) should return the current iterator and then advance the iterator. This is what I get as the output:
$ ./post
about to erase 8
about to erase 6
about to erase 4
about to erase 2
about to erase 0
0
2
4
6
8
If, however, I change the line a.erase((it++).base()); to a.erase((++it).base());, I get the correct behavior. Why is this?
Useful clarification: I am using base() since reverse_iterators cannot be used in erase(). There is an application where I want to go reverse on the vector to erase stuff.
The
base()of a reverse iterator is offset by 1. Sorbegin().base() == end()andrend().base() == begin().This is nothing other than the generalization of this reverse loop:
The loop traverses the array in reverse order, and note how we need a
- 1on the “iterator”.Update: Now let’s investigate what a reverse iterator is: It is simply a wrapper around a bidirectional iterator. Suppose the reverse iterator is called
it; then it has an ordinary iterator memberit.base(). For example,v.rbegin().base() == v.end(). When you say++it, that just calls--it.base()(conceptually). The real magic is the dereference operation: This has to give us one element before the underlying iterator:This is exactly the same arithmetic which told us that the i th element from the back of an array is offset by one:
array[N - i - 1]. This also shows us why we need a bidirectional iterator to form reverse iterators.Now it is clear how we can erase via reverse iterators from a container that does not invalidate iterators, such as any node-based container:
Remember that this requires that erasing does not invalidate any iterators other than the erasee, like in any node-based container. Erasing like this from a vector would be even more difficult. For a vector, erasing invalidates all iterators past the erasee (in forward direction), so we have to use the return value of the
erasefunction:In a picture (we’re removing element “5”):