Share via


reverse_iterator

reverse_iterator

template<class RanIt,
    class T = iterator_traits<RanIt>::value_type,
    class Ref = T&,
    class Ptr = T *, class Dist = ptrdiff_t>
    class reverse_iterator
        : public iterator<random_access_iterator_tag, T, Dist> {
public:
    typedef RanIt iter_type;
    typedef Ref reference_type;
    typedef Ptr pointer_type;
    reverse_iterator();
    explicit reverse_iterator(RanIt x);
    RanIt base() const;
    Ref operator*() const;
    Ptr operator->() const;
    reverse_iterator& operator++();
    reverse_iterator operator++(int);
    reverse_iterator& operator--();
    reverse_iterator operator--();
    reverse_iterator& operator+=(Dist n);
    reverse_iterator operator+(Dist n) const;
    reverse_iterator& operator-=(Dist n);
    reverse_iterator operator-(Dist n) const;
    Ref operator[](Dist n) const;
protected:
    RanIt current;
    };

The template class describes an object that behaves like a random-access iterator of class iterator<random_access_iterator_tag, ``T``, ``Dist``>. It stores a random-access iterator of type RanIt in the protected object current. Incrementing the object x of type reverse_iterator decrements x.current, and decrementing x increments x.current. Moreover, the expression *x evaluates to *(current - 1), of type Ref. Typically, Ref is type T&.

Thus, you can use an object of class reverse_iterator to access in reverse order a sequence that is traversed in order by a random-access iterator.