public member function
<iterator>

std::reverse_iterator::reverse_iterator

default (1)
reverse_iterator();
initialization (2)
explicit reverse_iterator (iterator_type it);
copy (3)
template <class Iter>
  reverse_iterator (const reverse_iterator<Iter>& rev_it);
Constructs reverse_iterator object
Constructs a reverse iterator object:

(1) default constructor
Constructs a reverse iterator that points to no object.
The internal base iterator is value-initialized.
(2) initalization constructor
Constructs a reverse iterator from some original iterator it. The behavior of the constructed object replicates the original, except that it iterates through its pointed elements in the reverse order.
(3) copy / type-cast constructor
Constructs a reverse iterator from some other reverse iterator. The constructed object keeps the same sense of iteration as rev_it.

Parameters

it
An iterator, whose sense of iteration is inverted in the constructed object.
Member type iterator_type is the underlying bidirectional iterator type (the class template parameter: Iterator).
rev_it
An iterator of a reverse_iterator type, whose sense of iteration is preserved.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// reverse_iterator example
#include <iostream>     // std::cout
#include <iterator>     // std::reverse_iterator
#include <vector>       // std::vector

int main () {
  std::vector<int> myvector;
  for (int i=0; i<10; i++) myvector.push_back(i);

  typedef std::vector<int>::iterator iter_type;
                                                         // ? 0 1 2 3 4 5 6 7 8 9 ?
  iter_type from (myvector.begin());                     //   ^
                                                         //         ------>
  iter_type until (myvector.end());                      //                       ^
                                                         //
  std::reverse_iterator<iter_type> rev_until (from);     // ^
                                                         //         <------
  std::reverse_iterator<iter_type> rev_from (until);     //                     ^

  std::cout << "myvector:";
  while (rev_from != rev_until)
    std::cout << ' ' << *rev_from++;
  std::cout << '\n';

  return 0;
}


Output:

myvector: 9 8 7 6 5 4 3 2 1 0

Exception safety

Provides the same level of guarantee as the proper constructor of the base iterator.

See also