Draft 2002-08-22

<iterator>

The <iterator> header declares classes and functions for defining and using iterators. Read about iterators in Chapter 11. Iterators are especially important when using the standard algorithms in <algorithm>.

An iterator gives a program access to the contents of a container or other sequences, such as I/O streams. You can think of an iterator as a sophisticated pointer; the syntax for using iterator resembles that of pointers. An iterator points to a single element in a container or sequence and can be advanced to the next element with the ++ (increment) operator. The unary * (dereference) operator returns the element that the iterator points to. Iterators (except for output iterators) can be compared: two iterators are equal if they point to the same position in the same sequence.

There are five kinds or categories of iterators: input, output, forward, bidirectional, and random access.

An input iterator permits one pass to read a sequence. The increment operator advances to the next element, but there is no decrement operator. The dereference operator does not return an lvalue, so you can read elements but not modify them.

An output iterator permits one pass to write a sequence. The increment operator advances to the next element, but there is no decrement operator. You can dereference an element only to assign a value to it. You cannot compare output iterators.

A forward iterator is like a combination of an input and an output iterator. You can use a forward iterator anywhere an input iterator is required or where an output iterator is required. A forward iterator, as its name implies, permits unidirectional access to a sequence, but you can refer to a single element and modify it multiple times before advancing the iterator.

A bidirectional iterator is like a forward iterator but also supports the -- (decrement) operator to move the iterator backward by one position.

A random access iterator is like a bidirectional iterator but also supports the [] (subscript) operator to access any index in the sequence. Also, you can add or subtract an integer to move a random access integer by more than one position at a time. Subtracting two random access iterators yields an integer distance between them. Thus, a random access iterator is most like a conventional pointer,a nd in fact, a pointer can be used as a random access iterator.

An input, forward, bidirectional, or random access iterator can be a constant iterator. Dereferencing a constant iterator yields an rvalue, not an lvalue.

To create your own iterator class, see the iterator class template.

advance function template

Move iterator forward or backward

template <typename InputIterator, typename Distance>
  void advance(InputIterator& i, Distance n);

The advance function template advances any input iterator i by the distance n. If the iterator is bidirectional or random access, n can be negative. If the iterator is random access, the advance function is specialized as i + n; other iterators apply the ++ operator n times (or -- for a bidirectional iterator when n is negative).

See Also

distance function template

back_insert_iterator class template

Output iterator to push items back onto a container

template <typename Container>
  class back_insert_iterator :
    public iterator<output_iterator_tag,void,void,void,void>
{
protected:
  Container* container;
public:
  typedef Container container_type;
  explicit back_insert_iterator(Container& x);
  back_insert_iterator<Container>&
    operator=(typename Container::const_reference value);
  back_insert_iterator<Container>& operator*();
  back_insert_iterator<Container>& operator++();
  back_insert_iterator<Container> operator++(int);
};

The back_insert_iterator class template implements an output iterator that stores elements in a container by calling the container's push_back function. The most convenient way to create a back_insert_iterator object is to use the back_inserter function template.

explicit back_insert_iterator (Container& x)
The constructor initializes the container member with &x.
back_insert_iterator<Container>& operator=
(typename Container::const_reference value)
The assignment operator calls container->push_back(value). The return value is *this.
back_insert_iterator<Container>& operator* ()
The dereference operator returns *this.
back_insert_iterator<Container>& operator++ ()
back_insert_iterator<Container> operator++ (int)
The increment operators return *this.

See Also

back_inserter function template, front_insert_iterator class template, insert_iterator class template

back_inserter function template

Create a back_insert_iterator

template <typename Container>
 back_insert_iterator<Container> back_inserter(Container& x);

The back_inserter function template constructs a back_insert_iterator object for the container x. Example 13-19 shows how to use back_inserter to read integers from a file into a vector.

Example 13-19: Using a back_inserter to add numbers to a vector.

std::ifstream in("experiment.dat");
std::vector<int> data;
std::copy(std::istream_iterator<int>(in),
          std::istream_iterator<int>(),
          std::back_inserter(data));

See Also

back_insert_iterator class template, front_inserter function template, inserter function template

bidirectional_iterator_tag class

Tag for a bidirectional iterator

struct bidirectional_iterator_tag :
  public forward_iterator_tag {};

Use the bidirectional_iterator_tag class as the iterator category when declaring a new iterator class. When writing a generic algorithm or similar function, you can use the iterator's category to write specialized implementations for different kinds of iterators. See Example 13-20, under the distance function template, for an example.

See Also

bidirectional_iterator_tag class, forward_iterator_tag class, input_iterator_tag class, iterator class template, output_iterator_tag class, random_access_iterator_tag class

distance function template

Count elements between two iterators

template<typename InputIterator>
  typename iterator_traits<InputIterator>::difference_type
    distance(InputIterator first, InputIterator last);

The distance function returns the number of elements between first and last. The function is specialized for random access iterators to use the - operator; for other input iterators, the function applies the ++ operator to first until first == last. The behavior is undefined if first and last refer to different containers or if last points to an element earlier than first.

Example 13-20 shows a simple implementation of the distance function. The first specialized implementation works for any input iterator. The second one works only with random access iterators, and uses the subtraction operator to compute the distance in constant time.

Example 13-20: A simple implementation of distance.

namespace std {
  template<typename InputIter>
  typename iterator_traits<InputIter>::difference_type
  specialize_distance(InputIter first, InputIter last,
                      input_iterator_tag)
  {
    typename iterator_traits<InputIter>::difference_type n;
    for (n = 0; first != last; ++first)
      ++n;
    return n;
  }

  template<typename InputIter>
  typename iterator_traits<InputIter>::difference_type
  specialize_distance(InputIter first, InputIter last,
                      random_access_iterator_tag)
  {
    return last - first;
  }

  template<typename InputIter>
  typename iterator_traits<InputIter>::difference_type
  distance(InputIter first, InputIter last)
  {
    return specialize_distance(first, last,
      iterator_traits<InputIter>::iterator_category());
  }
}

See Also

advance function template

forward_iterator_tag class

Tag for a forward iterator

struct forward_iterator_tag : public input_iterator_tag {};

Use the forward_iterator_tag class as the iterator category when declaring a new iterator class. When writing a generic algorithm or similar function, you can use the iterator's category to write specialized implementations for different kinds of iterators. See Example 13-20, under the distance function template, for an example.

See Also

bidirectional_iterator_tag class, forward_iterator_tag class, input_iterator_tag class, iterator class template, output_iterator_tag class, random_access_iterator_tag class

front_insert_iterator class template

Iterator to insert elements at the front of a container

template <typename Container>
class front_insert_iterator :
  public iterator<output_iterator_tag,void,void,void,void>
{
protected:
  Container* container;
public:
  typedef Container container_type;
  explicit front_insert_iterator(Container& x);
  front_insert_iterator<Container>&
    operator=(typename Container::const_reference value);
  front_insert_iterator<Container>& operator*();
  front_insert_iterator<Container>& operator++();
  front_insert_iterator<Container> operator++(int);
};

The front_insert_iterator class template implements an output iterator that stores elements in a container by calling the container's push_front function. The most convenient way to create a front_insert_iterator object is to use the front_inserter function template.

explicit front_insert_iterator (Container& x)
The constructor initializes the container member with &x.
front_insert_iterator<Container>& operator=
(typename Container::const_reference value)
The assignment operator calls container->push_front(value). The return value is *this.
front_insert_iterator<Container>& operator* ()
The dereference operator returns *this.
front_insert_iterator<Container>& operator++ ()
front_insert_iterator<Container> operator++ (int)
The increment operators return *this.

See Also

back_insert_iterator class template, front_inserter function template, insert_iterator class template

front_inserter function template

Create a front_insert_iterator

template <typename Container>
  front_insert_iterator<Container>
    front_inserter(Container& x);

The front_inserter function template constructs a front_insert_iterator object for the container x. Example 13-21 shows how to use front_inserter to read integers from a file into a list in reverse order.

Example 13-21: Using a front_inserter to add numbers to a list.

std::ifstream in("experiment.dat");
std::list<int> data;
std::copy(std::istream_iterator<int>(in),
          std::istream_iterator<int>(),
          std::front_inserter(data));

See Also

back_inserter function template, front_insert_iterator class template, inserter function template

input_iterator_tag class

Tag for an input iterator

struct input_iterator_tag {};

Use the input_iterator_tag class as the iterator category when declaring a new iterator class. When writing a generic algorithm or similar function, you can use the iterator's category to write specialized implementations for different kinds of iterators. See Example 13-20, under the distance function template, for an example.

See Also

bidirectional_iterator_tag class, forward_iterator_tag class, iterator class template, output_iterator_tag class, random_access_iterator_tag class

insert_iterator class template

Iterator to insert elements in a container

class insert_iterator :
  public iterator<output_iterator_tag,void,void,void,void>
{
protected:
  Container* container;
  typename Container::iterator iter;
public:
  typedef Container container_type;
  insert_iterator(Container& cont,
    typename Container::iterator iter);
  insert_iterator<Container>&
    operator=(typename Container::const_reference value);
  insert_iterator<Container>& operator*();
  insert_iterator<Container>& operator++();
  insert_iterator<Container>& operator++(int);
};

The insert_iterator class template implements an output iterator that stores elements in a container by calling the container's insert function. The most convenient way to create a insert_iterator object is to use the inserter function template. Following are the member functions of insert_iterator:

insert_iterator (Container& x,

typename Container::iterator i)
The constructor initializes the container member with &x and iter with i. Thus, the elements to be inserted in the container will be inserted at position i.
insert_iterator<Container>& operator=
(typename Container::const_reference value)
The assignment operator performs the equivalent of the following:
iter = container->insert(iter, value);
++iter;
return *this;
insert_iterator<Container>& operator* ()
The dereference operator returns *this.
insert_iterator<Container>& operator++ ()
insert_iterator<Container> operator++ (int)
The increment operators return *this.

See Also

back_insert_iterator class template, front_insert_iterator class template, inserter function template

inserter function template

Create an insert_iterator

template <typename Container, typename Iterator>
  insert_iterator<Container>
    inserter(Container& x, Iterator i);

The inserter function template constructs an insert_iterator object for the container x to insert items starting at position i. Figure 13-22 illustrates a simple use of the inserter function.

Figure 13-22: Using inserter to insert a vector in the middle of another vector.

See Also

back_inserter function template, front_inserter function template, insert_iterator class template

istream_iterator class template

Input iterator to read items from an istream

template <typename T,
  typename charT = char,
  typename traits = char_traits<charT>,
  typename Distance = ptrdiff_t>
class istream_iterator :
  public iterator<input_iterator_tag,
    T, Distance, const T*, const T&>
{
public:
  typedef charT char_type;
  typedef traits traits_type;
  typedef basic_istream<charT,traits> istream_type;
  istream_iterator();
  istream_iterator(istream_type& stream);
  istream_iterator
    (const istream_iterator<T,charT,traits,Distance>& x);
  ~istream_iterator();
  const T& operator*() const;
  const T* operator->() const;
  istream_iterator<T,charT,traits,Distance>& operator++();
  istream_iterator<T,charT,traits,Distance> operator++(int);
};

The istream_iterator class template wraps an input iterator around an istream, making the stream appear to be a sequence of items, each of type T.

Example 13-21 (under front_inserter, earlier in this section) shows how an istream_iterator can be used to read a series of integers from a file.

Following are the member functions of istream_iterator:

istream_iterator ()
The default constructor constructs an istream_iterator that denotes the end of the stream. End-of-stream iterators are equal to each other and are not equal to any other istream_iterator.
istream_iterator (istream_type& stream)
The constructor constructs an istream_iterator to read from stream. The constructor might read the first item from the stream. An istream_iterator that wraps a stream is equal to an end-of-stream iterator when stream.eof() is true.
istream_iterator (const
istream_iterator<T,charT,traits,Distance>& iter)
The constructor constructs a copy of iter. Note that two istream_iterator objects are the same (operator== is true) if they point to the same stream object.
const T& operator* () const
The dereference operator returns the item that was read most recently from the stream.
const T* operator-> ()
const istream_iterator<T,charT,traits,Distance>&
operator++ ()
The pointer operator returns a pointer to the item that was read most recently from the stream.
istream_iterator<T,charT,traits,Distance>
operator++ (int)
The increment operator reads the next item from the stream, using operator>>. The return value is *this.

See Also

istreambuf_iterator class template, ostream_iterator class template, istream in <istream>

istreambuf_iterator class template

Input iterator to read characters from a streambuf

template<typename charT, typename traits=char_traits<charT> >
class istreambuf_iterator :
  public iterator<input_iterator_tag,
    charT, typename traits::off_type, charT*, charT&>
{
public:
  typedef charT char_type;
  typedef traits traits_type;
  typedef typename traits::int_type int_type;
  typedef basic_streambuf<charT,traits> streambuf_type;
  typedef basic_istream<charT,traits> istream_type;
  class proxy; // exposition only
public:
  istreambuf_iterator() throw();
  istreambuf_iterator(istream_type& s) throw();
  istreambuf_iterator(streambuf_type* s) throw();
  istreambuf_iterator(const proxy& p) throw();
  charT operator*() const;
  istreambuf_iterator<charT,traits>& operator++();
  proxy operator++(int);
  bool equal(istreambuf_iterator& b) const;
};

The istreambuf_iterator class template wraps a streambuf object as an input iterator to read characters from the stream buffer. Example 13-22 shows how to use streambuf iterators to copy files.

Example 13-22: Copying files using streambuf iterators.

void copyfile(const char* from, const char* to)
{
  std::ifstream in(from);
  std::ofstream out(to);

  std::copy(std::istreambuf_iterator<char>(in),
            std::istreambuf_iterator<char>(),
            std::ostreambuf_iterator<char>(out));
}

The post-increment operator (++) returns a proxy object. The proxy class is implementation-defined, but it must have the capability to return the character and the underlying stream buffer. This section assumes the class name is proxy. Example 13-23 shows a prototypical implementation of proxy.

Example 13-23: A trivial implementation of the proxy class

template<typename charT, typename traits=char_traits<charT> >
class istreambuf_iterator<charT, traits>::proxy
{
  charT keep;
  basic_streambuf<charT,traits>* sbuf;
  proxy(charT c, basic_streambuf<charT,traits>* sbuf);
    : keep(c), sbuf(sbuf) {}
public:
  charT operator*() { return keep; }
};

In the following descriptions of the member functions of istreambuf_iterator, the data member sbuf is a pointer to the iterator's stream buffer. The sbuf member serves only to keep the function descriptions clear and simple; the class is not required to have such a member, or a member with that name.

istreambuf_iterator () throw()
The default constructor constructs the end-of-stream iterator.
istreambuf_iterator (istream_type& s) throw()
istreambuf_iterator (streambuf_type* sb) throw()
istreambuf_iterator (const proxy& p) throw()
The constructor constructs an istreambuf_iterator and initializes sbuf to s.rdbuf(), sb, or p.sbuf. If sb == 0, an end-of-stream iterator is constructed.
charT operator* () const
The dereference operator returns sbuf->sbumpc().
istreambuf_iterator<charT,traits>& operator++ ()
The pre-increment operator calls sbuf->sbumpc() and returns *this.
proxy operator++ (int)
The post-increment operator returns proxy(sbuf->sbumpc(), sbuf).
bool equal (istreambuf_iterator& b) const
The equal function returns true if both iterators are end-of-stream iterators or if neither iterator is an end-of-stream iterator. The iterators do not have to use the same stream buffer.

See Also

istream_iterator class template, ostreambuf_iterator class template, streambuf in <streambuf>

iterator class template

Iterator base class template

template<typename Category,
         typename T,
         typename Distance = ptrdiff_t,
         typename Pointer = T*,
         typename Reference = T&>
struct iterator
{
  typedef T value_type;
  typedef Distance difference_type;
  typedef Pointer pointer;
  typedef Reference reference;
  typedef Category iterator_category;
};

The iterator class template is a convenient base class template to use when implementing your own iterator.

The Category template parameter must be one of the five iterator category tags: bidirectional_iterator_tag, forward_iterator_tag, input_iterator_tag, output_iterator_tag, or random_access_iterator_tag. The T parameter is the element type; Pointer is the pointer-to-element type, and Reference is the reference-to-element type. The Distance parameter is an integral type that represents the distance between two iterators. Distance and T are void for an output iterator.

See Also

iterator_traits class template, reverse_iterator class template

iterator_traits class template

Iterator traits

template<typename Iterator>
struct iterator_traits
{
  typedef typename Iterator::difference_type difference_type;
  typedef typename Iterator::value_type value_type;
  typedef typename Iterator::pointer pointer;
  typedef typename Iterator::reference reference;
  typedef typename Iterator::iterator_category
    iterator_category;
};

The iterator_traits class template declares traits for an iterator. If you use the iterator class template, the iterator traits are automatically defined for you.

If you write your own specialization, the iterator_category type must be one of the five iterator tag classes. For an output iterator, difference_type and value_type are void.

When writing a generic algorithm or other function that uses iterators, you can use the iterator_traits to specialize the behavior for certain kinds of iterators. See Example 13-20, under distance, earlier in this section, for an example of how iterator traits can be used to improve the performance of a function.

See Also

iterator class template

iterator_traits<T*> template specialization

Iterator traits specialized for pointers

template<typename  T>
struct iterator_traits<T*>
{
  typedef ptrdiff_t difference_type;
  typedef T value_type;
  typedef T* pointer;
  typedef T& reference;
  typedef random_access_iterator_tag iterator_category;
};

The iterator_traits class template is specialized for pointers. This specialization lets you use a pointer as a random access iterator.

See Also

iterator_traits class template

iterator_traits<const T*> template specialization

Iterator traits specialized for pointers to const

template<typename  T>
struct iterator_traits<const T*>
{
  typedef ptrdiff_t difference_type;
  typedef T value_type;
  typedef const T* pointer;
  typedef const T& reference;
  typedef random_access_iterator_tag iterator_category;
};

The iterator_traits class template is specialized for pointers to const. This specialization lets you use a pointer as a random access iterator.

See Also

iterator_traits class template

ostream_iterator class template

Output iterator to write items to an ostream

template <typename T,
  typename charT = char,
  typename traits = char_traits<charT> >
class ostream_iterator :
  public iterator<output_iterator_tag,void,void,void,void>
{
public:
 typedef charT char_type;
 typedef traits traits_type;
 typedef basic_ostream<charT,traits> ostream_type;
 ostream_iterator(ostream_type& s);
 ostream_iterator(ostream_type& s, const charT* delimiter);
 ostream_iterator(const ostream_iterator<T,charT,traits>& x);
 ~ostream_iterator();
 ostream_iterator<T,charT,traits>& operator=(const T& value);
 ostream_iterator<T,charT,traits>& operator*();
 ostream_iterator<T,charT,traits>& operator++();
 ostream_iterator<T,charT,traits>& operator++(int);
};

The ostream_iterator class template wraps an output iterator around an ostream, making the stream appear to be a sequence of items, each of type T. For example, suppose you have a vector of data. You can print the data, one number per line, by using an ostream_iterator, as shown in Example 13-24.

Example 13-24: Writing data using an ostream_iterator.

std::vector<int> data;
... // acquire data
std::copy(data.begin(), data.end(),
          std::ostream_iterator(std::cout, "\n"));

Following are the member functions of ostream_iterator:

ostream_iterator (ostream_type& stream)
ostream_iterator (ostream_type& stream, const charT* delimiter)
ostream_iterator (const ostream_iterator<T,charT,traits>& x)
The constructor prepares to write items to stream. If delimiter is present, it will be written after each item. The copy constructor copies from x the reference to the stream and the delimiter.
ostream_iterator<T,charT,traits>& operator= (const T& value)
The assignment operator writes value to the stream using operator<<. If the ostream_iterator has a delimiter, it is written after value. The return value is *this.
ostream_iterator<T,charT,traits>& operator *()
The dereferenc eoperator returns *this.
ostream_iterator<T,charT,traits>& operator ++()
ostream_iterator<T,charT,traits>& operator++ (int)
The increment operators return *this.

See Also

istream_iterator class template, ostreambuf_iterator class template, ostream in <ostream>

ostreambuf_iterator class template

Output iterator to write characters to a streambuf

template<typename charT, typename traits=char_traits<charT> >
class ostreambuf_iterator :
  public iterator<output_iterator_tag,void,void,void,void>
{
public:
  typedef charT char_type;
  typedef traits traits_type;
  typedef basic_streambuf<charT,traits> streambuf_type;
  typedef basic_ostream<charT,traits> ostream_type;
public:
  ostreambuf_iterator(ostream_type& s) throw();
  ostreambuf_iterator(streambuf_type* s) throw();
  ostreambuf_iterator& operator=(charT c);
  ostreambuf_iterator& operator*();
  ostreambuf_iterator& operator++();
  ostreambuf_iterator& operator++(int);
  bool failed() const throw();
};

The ostreambuf_iterator class template wraps a streambuf object as an output iterator to write characters to the stream buffer. Example 13-22 (under istreambuf_iterator, earlier in this section) shows how to use streambuf iterators to copy files.

In the following descriptions of the member functions of ostreambuf_iterator, the data member sbuf is a pointer to the iterator's stream buffer. The sbuf member serves only to keep the function descriptions clear and simple; the class is not required to have such a member, or a member with that name.

ostreambuf_iterator (ostream_type& s) throw()
ostreambuf_iterator (streambuf_type* sb) throw()
The constructor saves the stream buffer s.rdbuf() or sb.
ostreambuf_iterator& operator= (charT c)
The assignment operator calls sbuf->sputc(c) (only if failed() returns false) and returns *this.
ostreambuf_iterator& operator* ()
The dereference operator returns *this.
ostreambuf_iterator& operator++ ()
ostreambuf_iterator& operator++ (int)
The increment operators return *this.
bool failed () const throw()
The failed function returns true if sbuf->sputc() ever returned traits::eof(). Otherwise, it returns false.

See Also

istreambuf_iterator class template, ostream_iterator class template, streambuf in <streambuf>

output_iterator_tag class

Tag for an output iterator

struct output_iterator_tag {};

Use the output_iterator_tag class as the iterator category when declaring a new iterator class. When writing a generic algorithm or similar function, you can use the iterator's category to write specialized implementations for different kinds of iterators. See Example 13-20, under the distance function template, for an example.

See Also

bidirectional_iterator_tag class, forward_iterator_tag class, input_iterator_tag class, iterator class template, random_access_iterator_tag class

random_access_iterator_tag class

Tag for a random access iterator

struct random_access_iterator_tag :
  public bidirectional_iterator_tag {};

Use the random_access_iterator_tag class as the iterator category when declaring a new iterator class. When writing a generic algorithm or similar function, you can use the iterator's category to write specialized implementations for different kinds of iterators. See Example 13-20, under the distance function template, for an example.

See Also

bidirectional_iterator_tag class, forward_iterator_tag class, input_iterator_tag class, iterator class template, output_iterator_tag class

reverse_iterator class template

Iterator wrapper to reverse direction

template <typename 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>
{
protected:
  Iterator current;
public:
  typedef Iterator iterator_type;
  typedef typename iterator_traits<Iterator>::difference_type
    difference_type;
  typedef typename iterator_traits<Iterator>::reference
    reference;
  typedef typename iterator_traits<Iterator>::pointer
    pointer;
  reverse_iterator();
  explicit reverse_iterator(Iterator x);
  template <typename U>
  reverse_iterator(const reverse_iterator<U>& u);
  Iterator base() const;  // explicit
  reference operator*() const;
  pointer operator->() const;
  reverse_iterator& operator++();
  reverse_iterator operator++(int);
  reverse_iterator& operator--();
  reverse_iterator operator--(int);
  reverse_iterator operator+(difference_type n) const;
  reverse_iterator& operator+=(difference_type n);
  reverse_iterator operator-(difference_type n) const;
  reverse_iterator& operator-=(difference_type n);
  reference operator[](difference_type n) const;
};

The reverse_iterator class template is an adapter for a bidirectional or random access iterator to iterate the sequence in the opposite direction of the adapted iterator. In addition to the reverse_iterator template, there are also several function templates for the comparison and arithmetic (+ and -) operators.

The standard containers return reverse_iterator objects from the rbegin() and rend() functions. Example 13-25 shows a simple implementation of these functions.

Example 13-25: Implementing rbegin() and rend().

reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }

Because an iterator can point to one past the last item in a container, but cannot point to one item before the first, a reverse iterator points to one item before the position where the adapted iterator points, as illustrated in Figure 13-23.

Figure 13-23: How a reverse iterator works.

Following are the member functions of reverse_iterator:

reverse_iterator ()
explicit reverse_iterator (Iterator i)
template <typename U> reverse_iterator (const reverse_iterator<U>& ri)
The constructors initialize the current data member. The default constructor uses the default constructor for current. The other constructors initialize current with i or ri.current.
Iterator base () const
The base function returns the associated normal iterator.
reference operator *() const
The dereference operator returns a reference to the item that the reverse iterator logically points to, which is one item before where current points. Thus, you can think of the dereference function working as follows:
tmp = current;
--tmp;
return *tmp;
where tmp is a private data member of the reverse iterator.
pointer operator ->() const
The pointer operator returns a pointer to the item that the reverse iterator logically points to, that is, &(operator*()).
reverse_iterator& operator ++()
The pre-increment operator decrements current and returns *this.
reverse_iterator operator++ (int)
The post-increment saves a copy of *this, decrements current, and returns the saved item.
reverse_iterator& operator --()
The pre-decrement operator increments current and returns *this.
reverse_iterator operator-- (int)
The post-decrement operator saves a copy of *this, increments current, and returns the saved item.
reverse_iterator operator+ (difference_type n) const
The + operator returns reverse_iterator(current - n).
reverse_iterator& operator+= (difference_type n)
The += operator subtracts n from current and returns *this.
reverse_iterator operator- (difference_type n) const
The - operator returns reverse_iterator(current + n).
reverse_iterator& operator-= (difference_type n)
The -= operator adds n to current and returns *this.
reference operator[] (difference_type n) const
The subscript operator returns current[-n-1].

Following are several non-member functions for comparing reverse iterators and for performing basic arithmetic: finding the distance between two reverse iterators and advancing a reverse iterator by an integer.

template <typename Iterator>
bool operator== (const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
The equality operator returns true when the base iterators are equal, that is, x.current == y.current.
template <typename Iterator>
bool operator!= (const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
The inequality operator returns true when x and y have different base iterators, that is, x.current != y.current.
template <typename Iterator>
bool operator< (const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
The less-than operator returns true when x is closer than y to the beginning of the sequence. Because x and y are reverse iterators, the function returns y.current < x.current.
template <typename Iterator>
bool operator> (const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
The greater-than operator returns true when x is farther than y from the beginning of the sequence. Because x and y are reverse iterators, the function returns y.current > x.current.
template <typename Iterator>
bool operator>= (const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
The >= operator returns true when x is farther than y from the beginning of the sequence or x equals y. Because x and y are reverse iterators, the function returns y.current >= x.current.
template <typename Iterator>
bool operator<= (const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
The >= operator returns true when x is closer than y to the beginning of the sequence or x equals y. Because x and y are reverse iterators, the function returns y.current <= x.current.
template <typename Iterator>
typename reverse_iterator<Iterator>::difference_type
operator- (const reverse_iterator<Iterator>& x,
const reverse_iterator<Iterator>& y)
The subtraction operator returns the distance between two reverse iterators, that is, y.current - x.current.
template <typename Iter>
reverse_iterator<Iter>
operator+ (
typename reverse_iterator<Iter>::difference_type n,
const reverse_iterator<Iter>& ri)
The addition operator advances a reverse iterator ri by n. This function is a counterpart to the operator+ member function, allowing you to write ri + n and n + ri, which yield the same result.

See Also

iterator class template