Sunday, July 18, 2010

Reverse Iterators

With respect to iterators and reverse iterators, the iterator has the same physical and logical position, and the reverse iterator has a physical position different that then the logical position. Note that the Physical location is where the iterator really points, and logical location is where the value will be read or written.

This is because it is not valid to point to an element before the first element in a container, but you are allowed to point to the element after he last element in the container. When you call base(), it adjusts this discrepancy.

Below is an example of a vector of ints containing 0, 1, and 2:

          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. 267.

No comments:

Post a Comment