Namespaces
Variants
Views
Actions

std::make_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>
std::reverse_iterator<Iter> make_reverse_iterator( Iter i );
(since C++14)
(until C++17)
template <class Iter>
constexpr std::reverse_iterator<Iter> make_reverse_iterator( Iter i );
(since C++17)

make_reverse_iterator is a convenience function template that constructs a std::reverse_iterator for the given iterator i with the type deduced from the type of the argument.

Contents

[edit] Parameters

i - iterator to be converted to reverse iterator

[edit] Return value

A std::reverse_iterator constructed from i

[edit] Possible implementation

template< class Iter >
constexpr std::reverse_iterator<Iter> make_reverse_iterator( Iter i )
{
    return std::reverse_iterator<Iter>(i);
}

[edit] Example

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
 
int main() {
    std::vector<int> v{ 1, 3, 10, 8, 22 };
 
    std::sort(v.begin(), v.end());
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ", "));
 
    std::cout << '\n';
 
    std::copy(
        std::make_reverse_iterator(v.end()), 
        std::make_reverse_iterator(v.begin()),
        std::ostream_iterator<int>(std::cout, ", "));
}

Output:

1, 3, 8, 10, 22, 
22, 10, 8, 3, 1,

[edit] See also

iterator adaptor for reverse-order traversal
(class template) [edit]
returns a reverse iterator to a container or array
(function template) [edit]
(C++14)
returns a reverse end iterator for a container or array
(function template) [edit]