Apache C++ Standard Library Reference Guide

## reverse_iterator

Library: Iterators

    Does not inherit

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Example
- Complexity
- See Also
- Standards Conformance

### Local Index

No Entries

### Summary

An iterator adaptor that traverses a collection backwards.

### Synopsis

    #include <iterator>

    namespace std {
      template <class Iterator>
      class reverse_iterator;
    }

### Description

The class template reverse_iterator adapts any random access iterator or bidirectional_iterator to facilitate the traversal of collections in the opposite direction. The fundamental relationship between a reverse iterator and its corresponding iterator `i` is established by the identity:

    &*(reverse_iterator(i)) == &*(i - 1);

This mapping is necessary because, while an iterator past the end of a sequence is still valid but not dereferenceable, there might not be a valid iterator before its beginning.

### Interface

    namespace std {

      template <class Iterator>
      class reverse_iterator
        : public iterator
           <typename iterator_traits<iterator>::iterator_category,
           <typename iterator_traits<iterator>::value_type,
           <typename iterator_traits<iterator>::difference_type,
           <typename iterator_traits<iterator>::pointer,
           <typename iterator_traits<iterator>::reference>
      {

        public:
          typedef Iterator iterator_type;

          reverse_iterator();
          explicit reverse_iterator(iterator_type);

          iterator_type base();

          reference operator*();

          reverse_iterator& operator++();
          reverse_iterator  operator++(int);
          reverse_iterator& operator--();
          reverse_iterator  operator--(int);

          reverse_iterator  operator+  (difference_type) const;
          reverse_iterator& operator+= (difference_type);
          reverse_iterator operator-   (difference_type) const;
          reverse_iterator& operator-= (difference_type);

          reference operator[] (difference_type) const;
      };

      // Nonmember Operators

      template <class Iterator>
      bool operator== (const reverse_iterator<Iterator>&,
                       const reverse_iterator<Iterator>&);

      template <class Iterator>
      bool operator!= (const reverse_iterator<Iterator>&,
                       const reverse_iterator<Iterator>&);

      template <class Iterator>
      bool operator< (const reverse_iterator<Iterator>&,
                      const reverse_iterator<Iterator>&);

      template <class Iterator>
      bool operator> (const reverse_iterator<Iterator>&,
                      const reverse_iterator<Iterator>&);

      template <class Iterator>
      bool operator<= (const reverse_iterator<Iterator>&,
                       const reverse_iterator<Iterator>&);

      template <class Iterator>
      bool operator>= (const reverse_iterator<Iterator>&,
                       const reverse_iterator<Iterator>&);

      template <class Iterator>
      typename reverse_iterator<Iterator>::difference_type
      operator- (const reverse_iterator<Iterator>&,
                 const reverse_iterator<Iterator>&);

      template <class Iterator>
      reverse_iterator<Iterator>
      operator+ (
          typename reverse_iterator<Iterator>::difference_type,
          const reverse_iterator<Iterator>&);
    }

### Example

    //
    //  rev_itr.cpp
    //

    #include <iostream>   // for cout, endl
    #include <vector>     // for vector

    int main ()
    {
        typedef std::vector<int, std::allocator<int> > Vector;

        // Initialize a vector using an array.
        const Vector::value_type arr[] = { 3, 4, 7, 8 };
        const Vector v (arr + 0, arr + sizeof arr / sizeof *arr);

        // Output the original vector.
        std::cout << "Traversing vector with iterator: \n     ";
        for (Vector::const_iterator i = v.begin ();
             i != v.end (); ++i)
            std::cout << *i << " ";

        // Output the vector backwards.
        std::cout << "\n\nSame vector, same loop, "
                  << "reverse_iterator: \n     ";
        for (Vector::const_reverse_iterator j (v.end ());
             j != Vector::const_reverse_iterator (v.begin ());
             ++j)
            std::cout << *j << " ";

        std::cout << std::endl;

        return 0;
    }

    Program Output:

    Traversing vector with iterator:
         3 4 7 8

    Same vector, same loop, reverse_iterator:
         8 7 4 3

### Complexity

All iterator operations are required to take at most amortized constant time.

### See Also

Iterators

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 24.4.1.1