Is it possible to iterate a vector from the end to the beginning?
for (vector<my_class>::iterator i = my_vector.end();
i != my_vector.begin(); /* ?! */ ) {
}
Or is that only possible with something like that:
for (int i = my_vector.size() - 1; i >= 0; --i) {
}
One way is:
rbegin()/rend()were especially designed for that purpose. (And yes, incrementing areverse_iteratormoves it backward.)Now, in theory, your method (using
begin()/end()&--i) would work,std::vector‘s iterator being bidirectional, but remember,end()isn’t the last element — it’s one beyond the last element, so you’d have to decrement first, and you are done when you reachbegin()— but you still have to do your processing.