< cpp‎ | iterator
Revision as of 14:55, 2 November 2012 by P12bot (Talk | contribs)


Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <iterator>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class Iterator >

class reverse_iterator : public std::iterator<
                           typename std::iterator_traits<Iterator>::iterator_category,
                           typename std::iterator_traits<Iterator>::value_type,
                           typename std::iterator_traits<Iterator>::difference_type,
                           typename std::iterator_traits<Iterator>::pointer,

                           typename std::iterator_traits<Iterator>::reference >

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

std::reverse_iterator is an iterator adaptor that reverses the direction of a given iterator. In other words, when provided with a bidirectional iterator, std::reverse_iterator produces a new iterator that moves from the end to the beginning of the sequence defined by the underlying bidirectional iterator.

For a reverse iterator r constructed from an iterator i, the relationship &*r == &*(i-1) is always true; thus a reverse iterator constructed from a one-past-the-end iterator dereferences to the last element in a sequence.

This is the iterator returned by member functions rbegin() and rend() of the standard library containers.


Member types

Member type Definition
iterator_type Iterator
difference_type std::iterator_traits<Iterator>::difference_type
pointer std::iterator_traits<Iterator>::pointer
reference std::iterator_traits<Iterator>::reference

Member functions

Template:cpp/iterator/adaptor/dcl list constructorTemplate:cpp/iterator/adaptor/dcl list operator=Template:cpp/iterator/adaptor/dcl list baseTemplate:cpp/iterator/adaptor/dcl list operator*Template:cpp/iterator/adaptor/dcl list operator atTemplate:cpp/iterator/adaptor/dcl list operator arith

Member objects

Member name Definition
current (protected) a copy of the base() iterator

In addition to the current value of the underlying iterator, a typical implementation of std::reverse_iterator holds a decremented copy of the underlying iterator, which is used in dereferencing.

Non-member functions

Template:cpp/iterator/adaptor/dcl list operator cmpTemplate:cpp/iterator/adaptor/dcl list operator+Template:cpp/iterator/adaptor/dcl list operator-

Member types

Member type Definition
value_type std::iterator_traits<Iterator>::value_type
difference_type std::iterator_traits<Iterator>::difference_type
pointer std::iterator_traits<Iterator>::pointer
reference std::iterator_traits<Iterator>::reference
iterator_category std::iterator_traits<Iterator>::iterator_category

Note: before C++17, these member types are required to be obtained by inheriting from std::iterator< std::iterator_traits<Iterator>::iterator_category
, std::iterator_traits<Iterator>::value_type
, std::iterator_traits<Iterator>::difference_type
, std::iterator_traits<Iterator>::pointer
, std::iterator_traits<Iterator>::reference


#include <iostream>
#include <string>
#include <iterator>
int main()
    std::string s = "Hello, world";
    std::reverse_iterator<std::string::iterator> r = s.rbegin();
    r[7] = 'O'; // replaces 'o' with 'O' 
    r += 7; // iterator now points at 'O'
    std::string rev(r, s.rend());
    std::cout << rev << '\n';



See also

Template:cpp/iterator/dcl list iterator