Namespaces
Variants
Views
Actions

std::reverse_iterator

From cppreference.com
< cpp‎ | iterator
 
 
Iterator library
Iterator concepts
(C++20)
(C++20)
(C++20)
(C++20)
Iterator primitives
Iterator adaptors
Stream iterators
Iterator customization points
Iterator operations
(C++11)
(C++11)
Range access
(C++11)(C++14)
(C++11)(C++14)
(C++17)(C++20)
(C++14)(C++14)
(C++14)(C++14)
(C++17)
(C++17)
 
 
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,
                           typename std::iterator_traits<Iter>::difference_type,
                           typename std::iterator_traits<Iter>::pointer,

                           typename std::iterator_traits<Iter>::reference >
(until C++17)
template< class Iter >
class reverse_iterator;
(since C++17)

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.

range-rbegin-rend.svg

Contents

[edit] Member types

Member type Definition
iterator_type Iter
iterator_concept
(since C++20)
If Iter models std::RandomAccessIterator, this is std::random_access_iterator_tag. Otherwise, this is std::bidirectional_iterator_tag
iterator_category

std::iterator_traits<Iter>::iterator_category

(until C++20)

If std::iterator_traits<Iter>::iterator_category models std::DerivedFrom<std::random_access_iterator_tag>, this is std::random_access_iterator_tag. Otherwise, this is std::iterator_traits<Iter>::iterator_category unchanged

(since C++20)
value_type

std::iterator_traits<Iter>::value_type

(until C++20)

std::iter_value_t<Iter>

(since C++20)
difference_type

std::iterator_traits<Iter>::difference_type

(until C++20)

std::iter_difference_t<Iter>

(since C++20)
pointer std::iterator_traits<Iter>::pointer
reference

std::iterator_traits<Iter>::reference

(until C++20)

std::iter_reference_t<Iter>

(since C++20)

Member types iterator_category, value_type, difference_type, pointer and reference 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
, std::iterator_traits<Iter>::pointer
, std::iterator_traits<Iter>::reference
>
.

(until C++17)

[edit] Member functions

constructs a new iterator adaptor
(public member function) [edit]
assigns another iterator
(public member function) [edit]
accesses the underlying iterator
(public member function) [edit]
accesses the pointed-to element
(public member function) [edit]
accesses an element by index
(public member function) [edit]
advances or decrements the iterator
(public member function) [edit]

[edit] Member objects

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

[edit] Non-member functions

creates a std::reverse_iterator of type inferred from the argument
(function template) [edit]
compares the underlying iterators
(function template) [edit]
advances the iterator
(function template) [edit]
computes the distance between two iterator adaptors
(function template) [edit]
(C++20)
casts the result of dereferencing the adjusted underlying iterator to its associated rvalue reference type
(function template) [edit]
(C++20)
swap the objects pointer by two adjusted underlying iterators
(function template) [edit]

[edit] Notes

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.

[edit] Example

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

Output:

OlleH

[edit] See also

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