reverse_iterator Class

The template class is an iterator adaptor that describes a reverse iterator object that behaves like a random-access or bidirectional iterator, only in reverse. It enables the backward traversal of a range.

For a list of all members of this type, see reverse_iterator Members.

template <class RandomIterator>
class reverse_iterator


  • RandomIterator
    The type that represents the iterator to be adapted to operate in reverse.


Existing Standard Template Library containers also define reverse_iterator and const_reverse_iterator types and have member functions rbegin and rend that return reverse iterators. These iterators have overwrite semantics. The reverse_iterator adaptor supplements this functionality as offers insert semantics and can also be used with streams.

The reverse_iterators that require a bidirectional iterator must not call any of the member functions operator+=, operator+, operator-=, operator-, or operator[], which may only be used with random-access iterators.

If the range of an iterator is [_First, _Last), where the square bracket on the left indicates the inclusion on _First and the parenthesis on the right indicates the inclusion of elements up to _Left but excluding _Left itself. The same elements are included in the reversed sequence [rev – _First, rev – _Left) so that if _Left is the one-past-the-end element in a sequence, then the first element rev – _First in the reversed sequence points to *(_Left – 1 ). The identity which relates all reverse iterators to their underlying iterators is:

&*(reverse_iterator ( i ) ) == &*( i – 1 ).

In practice, this means that in the reversed sequence the reverse_iterator will refer to the element one position beyond (to the right of) the element that the iterator had referred to in the original sequence. So if an iterator addressed the element 6 in the sequence (2, 4, 6, 8), then the reverse_iterator will address the element 4 in the reversed sequence (8, 6, 4, 2).


Header: <iterator>

Namespace: std

See Also


Thread Safety in the Standard C++ Library

Standard Template Library

Other Resources

reverse_iterator Members

<iterator> Members