std::reverse_iterator
Defined in header <iterator>
|
||
template< class Iter > class reverse_iterator; |
||
std::reverse_iterator
is an iterator adaptor that reverses the direction of a given iterator, which must be at least a LegacyBidirectionalIterator or model bidirectional_iterator
(since C++20). 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.
Contents |
[edit] Member types
|
(until C++20) | ||||||||||||||||
|
(since C++20) |
Member types |
(until C++17) |
[edit] Member functions
constructs a new iterator adaptor (public member function) | |
assigns another iterator (public member function) | |
accesses the underlying iterator (public member function) | |
accesses the pointed-to element (public member function) | |
accesses an element by index (public member function) | |
advances or decrements the iterator (public member function) |
[edit] Member objects
Member name | Definition |
current (protected)
|
the underlying iterator of which base() returns a copy
|
[edit] Non-member functions
compares the underlying iterators (function template) | |
advances the iterator (function template) | |
computes the distance between two iterator adaptors (function template) | |
(C++20) |
casts the result of dereferencing the adjusted underlying iterator to its associated rvalue reference type (function template) |
(C++20) |
swaps the objects pointed to by two adjusted underlying iterators (function template) |
(C++14) |
creates a std::reverse_iterator of type inferred from the argument (function template) |
[edit] Helper templates
template< class Iterator1, class Iterator2 > requires (!std::sized_sentinal_for<Iterator1, Iterator2>) |
(since C++20) | |
This partial specialization of std::disable_sentinel_for
prevents specializations of reverse_iterator
from satisfying sized_sentinel_for
if their underlying iterators do not satisfy the concept.
[edit] Notes
std::reverse_iterator
does not work with iterators whose dereference returns a reference to a member of *this
(so-called "stashing iterators"). An example of a 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
(C++14) |
creates a std::reverse_iterator of type inferred from the argument (function template) |
(deprecated in C++17) |
base class to ease the definition of required types for simple iterators (class template) |