Friday, July 16, 2010

Reverse Iterators


If v is an object of type vector<int>, then
 v.rbegin()==vector<int>::reverse_iterator(v.end())   &&
 v.rend()  ==vector<int>::reverse_iterator(v.begin()) &&
 v.begin() ==v.rend().base()                          &&
 v.end()   ==v.rbegin().base()

Notice the base() calls to adjust the iterators as shown in the following diagram:

           begin()                     end()
           |Logical                    |Logical
           |Physical                   |Physical
           V                           V
x          0         1       2         y
^          ^                 ^         ^
|Logical   |Physical         |Logical  |Physical 
|          |                 |         | 
rend()     rend()            rbegin()  rbegin()

Reference: The C++ Standard Library: A Tutorial and Reference by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 269. 

No comments:

Post a Comment