I cannot answer the question “why”, but answer “how” - you should call base() on your iterator. It is going to bring the correct iterator forward.
In doing so, consider the relationship between reverse and forward iterators. this may be confusing at first, but actually quite simple. If you have a std::vector containing the following elements:
1, 2, 3, 4, 5
And you have reverse_iterator rit, which when dereferenced gives you 3, then *(rit.base) will be 4. To understand why, just remember that in normal iterators begin() is dereferenced, but end() not. In reverse iterators, the property must be the same - rbegin() must be dereferenced, but rend() must not - that is, it must point outside the beginning of the container.
Since, by definition, rend.base() same as begin() (since rend can be constructed as reverse_iterator(begin()) , the only way this can be rend.base() if rend.base() returns the next right element one after the start - begin() . It is easy to see that the same symmetry holds for rend() .
SergeyA
source share