Namespaces
Variants
Views
Actions

std::reverse_iterator

From cppreference.com
Defined in header <iterator>
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 >

std::reverse_iterator is an iterator adaptor which iterates from the end of the sequence defined by the underlying bidirectional iterator to the beginning of that sequence. For 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.

Contents

[edit] 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

[edit] 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.

[edit] Member functions

constructs a new reverse iterator
(public member function) [edit]
assigns contents
(public member function) [edit]
accesses the underlying iterator
(public member function) [edit]
dereferences the decremented underlying iterator
(public member function) [edit]
accesses an element by index
(public member function) [edit]
advances the iterator
(public member function)
decrements the iterator
(public member function)

[edit] Non-member functions

compares two reverse_iterators for equality
(function template)
orders reverse_iterators
(function template)
obtains the distance between two reverse_iterators
(function template)
advances a reverse_iterator
(function template)

Inherited from std::iterator

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

[edit] Example

#include <iostream>
#include <string>
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';
}

Output:

OlleH

[edit] See also

the basic iterator
(class template) [edit]