std::reverse_iterator (3) - Linux Manuals

std::reverse_iterator: std::reverse_iterator


std::reverse_iterator - std::reverse_iterator


Defined in header <iterator>
template< class Iter >
class reverse_iterator : public std::iterator<
typename std::iterator_traits<Iter>::iterator_category,
typename std::iterator_traits<Iter>::value_type, (until C++17)
typename std::iterator_traits<Iter>::difference_type,
typename std::iterator_traits<Iter>::pointer,
typename std::iterator_traits<Iter>::reference >
template< class Iter > (since C++17)
class reverse_iterator;

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 (as long as r is dereferenceable); 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
value_type std::iterator_traits<Iter>::value_type
difference_type std::iterator_traits<Iter>::difference_type
pointer std::iterator_traits<Iter>::pointer
reference std::iterator_traits<Iter>::reference
iterator_category std::iterator_traits<Iter>::iterator_category

These member types are required to be obtained by inheriting from std::iterator< std::iterator_traits<Iter>::iterator_category
, std::iterator_traits<Iter>::value_type
, std::iterator_traits<Iter>::difference_type (until C++17)
, std::iterator_traits<Iter>::pointer
, std::iterator_traits<Iter>::reference

Member type Definition
iterator_type Iter

Member functions

                constructs a new iterator adaptor
constructor (public member function)
                assigns another iterator
operator= (public member function)
                accesses the underlying iterator
base (public member function)
                accesses the pointed-to element
operator* (public member function)
                accesses an element by index
operator[] (public member function)

operator+= advances or decrements the iterator
operator+ (public member function)

Member objects

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

Non-member functions

make_reverse_iterator creates a std::reverse_iterator of type inferred from the argument
                      (function template)

operator!= compares the underlying iterators
operator< (function template)
                      advances the iterator
operator+ (function template)
                      computes the distance between two iterator adaptors
operator- (function template)


std::reverse_iterator does not work with iterators that return a reference to a member object (so-called "stashing iterators"). An example of stashing iterator is std::filesystem::path::iterator.


// Run this code

  #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

iterator base class to ease the definition of required types for simple iterators
                      (class template)
(deprecated in C++17)