Partager via


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.

template <class RandomIterator>
class reverse_iterator

Parameters

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

Remarks

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

Constructors

reverse_iterator

Constructs a default reverse_iterator or a reverse_iterator from an underlying iterator.

Typedefs

difference_type

A type that provides the difference between two reverse_iterators referring to elements within the same container.

iterator_type

A type that provides the underlying iterator for a reverse_iterator.

pointer

A type that provides a pointer to an element addressed by a reverse_iterator.

reference

A type that provides a reference to an element addressed by a reverse_iterator.

Member Functions

base

Recovers the underlying iterator from its reverse_iterator.

Operators

operator*

Returns the element that a reverse_iterator addresses.

operator+

Adds an offset to an iterator and returns the new reverse_iterator addressing the inserted element at the new offset position.

operator++

Increments the reverse_iterator to the next element.

operator+=

Adds a specified offset from a reverse_iterator.

operator-

Subtracts an offset from a reverse_iterator and returns a reverse_iterator addressing the element at the offset position.

operator--

Decrements the reverse_iterator to the previous element.

operator-=

Subtracts a specified offset from a reverse_iterator.

operator->

Returns a pointer to the element addressed by the reverse_iterator.

operator[]

Returns a reference to an element offset from the element addressed by a reverse_iterator by a specified number of positions.

Requirements

Header: <iterator>

Namespace: std

See Also

Reference

<iterator>

Thread Safety in the C++ Standard Library

Standard Template Library