Reverse iterators

reverse_bidirectional_iterator Bidirectional Iterator
reverse_iterator Random Access Iterator

Bidirectional and random access iterators have corresponding reverse iterator adaptors that iterate through the data structure in the opposite direction. They have the same signatures as the corresponding iterators. The fundamental relation between a reverse iterator and its corresponding iterator i is established by the identity


&*(reverse_iterator(i)) == &*(i - 1).
This mapping is dictated by the fact that while there is always a pointer past the end of an array, there might not be a valid pointer before the beginning of an array.

reverse_bidirectional_iterator () Constructor on reverse_bidirectional_iterator
reverse_bidirectional_iterator (BidirectionalIterator x) Constructor on reverse_bidirectional_iterator
* Operator on reverse_bidirectional_iterator
++ Operator on reverse_bidirectional_iterator
-- Operator on reverse_bidirectional_iterator
== Operator on reverse_bidirectional_iterator

reverse_iterator () Constructor on reverse_iterator
reverse_iterator (RandomAccessIterator x) Constructor on reverse_iterator
* Operator on reverse_iterator
++ Operator on reverse_iterator
-- Operator on reverse_iterator
+ Operator on reverse_iterator
+= Operator on reverse_iterator
- Operator on reverse_iterator
-= Operator on reverse_iterator
[] Operator on reverse_iterator
== Operator on reverse_iterator
< Operator on reverse_iterator

template 















class reverse_bidirectional_iterator
: public bidirectional_iterator {
    typedef reverse_bidirectional_iterator self;
    friend bool operator==(const self& x, const self& y);

protected:
    BidirectionalIterator current;

public:
    reverse_bidirectional_iterator() {}
    reverse_bidirectional_iterator(BidirectionalIterator x) : current(x) {}
    BidirectionalIterator base() { return current; }
    Reference operator*() const {
        BidirectionalIterator tmp = current;
        return *--tmp;
    }
    self& operator++() { --current; return *this; }
    self operator++(int) { self tmp = *this; --current; return tmp; }
    self& operator--() { ++current; return *this; }
    self operator--(int) { self tmp = *this; ++current; return tmp; }
};

template 















inline bool operator==(const reverse_bidirectional_iterator<
                             BidirectionalIterator, T,
                             Reference, Distance>& x,
                       const reverse_bidirectional_iterator<
                             BidirectionalIterator, T,
                             Reference, Distance>& y) {
    return x.current == y.current;
}

template 















class reverse_iterator : public random_access_iterator {
    typedef reverse_iterator
        self;
    friend bool operator==(const self& x, const self& y);
    friend bool operator<(const self& x, const self& y);
    friend Distance operator-(const self& x, const self& y);
    friend self operator+(Distance n, const self& x);

protected:
    RandomAccessIterator current;

public:
    reverse_iterator() {}
    reverse_iterator(RandomAccessIterator x) : current(x) {}
    RandomAccessIterator base() { return current; }
    Reference operator*() const {
        RandomAccessIterator tmp = current;
        return *--tmp;
    }
    self& operator++() { --current; return *this; }
    self operator++(int) { self tmp = *this; --current; return tmp; }
    self& operator--() { ++current; return *this; }
    self operator--(int) { self tmp = *this; ++current; return tmp; }
    self operator+(Distance n) const { return self(current - n); }
    self& operator+=(Distance n) { current -= n; return *this; }
    self operator-(Distance n) const { return self(current + n); }
    self& operator-=(Distance n) { current += n; return *this; }
    Reference operator[](Distance n) { return *(*this + n); }
};

template 















inline bool operator==(const reverse_iterator& x,
                       const reverse_iterator& y) {
    return x.current == y.current;
}

template 















inline bool operator<(const reverse_iterator& x,
                      const reverse_iterator& y) {
    return y.current < x.current;
}

template 















inline Distance operator-(const reverse_iterator& x,
                          const reverse_iterator& y) {
    return y.current - x.current;
}

template 















inline reverse_iterator
    operator+(Distance n,
              const reverse_iterator& x) {
        return reverse_iterator(x.current - n);
}