Namespaces
Variants
Views
Actions

std::ranges::reverse

From cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms: std::ranges::copy, std::ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)

Permutations
Numeric operations
C library
 
Constrained algorithms
Non-modifying sequence operations
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
Sorting operations
Binary search operations
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutations
 
Defined in header <algorithm>
Call signature
template<std::bidirectional_iterator I, std::sentinel_for<I> S>

  requires std::permutable<I>
    constexpr I

      ranges::reverse( I first, S last );
(1) (since C++20)
template<ranges::bidirectional_range R>

  requires std::permutable<ranges::iterator_t<R>>
    constexpr ranges::borrowed_iterator_t<R>

      ranges::reverse( R&& r );
(2) (since C++20)
1) Reverses the order of the elements in the range [first, last).
Behaves as if applying ranges::iter_swap to every pair of iterators first+i, last-i-1 for each integer i, where 0 ≤ i < (last-first)/2.
2) Same as (1), but uses r as the range, as if using ranges::begin(r) as first and ranges::end(r) as last.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

[edit] Parameters

first, last - the range of elements to reverse
r - the range of elements to reverse

[edit] Return value

An iterator equal to last.

[edit] Complexity

Exactly (last - first)/2 swaps.

[edit] Possible implementation

struct reverse_fn {
  template<std::bidirectional_iterator I, std::sentinel_for<I> S>
    requires std::permutable<I>
      constexpr I operator()( I first, S last ) const {
          auto last2{ ranges::next(first, last) };
          for (auto tail{ last2 }; !(first == tail or first == --tail); ++first) {
              ranges::iter_swap(first, tail);
          }
          return last2;
      }
 
  template<ranges::bidirectional_range R>
    requires std::permutable<ranges::iterator_t<R>>
      constexpr ranges::borrowed_iterator_t<R>
        operator()( R&& r ) const {
            return (*this)(ranges::begin(r), ranges::end(r));
        }
};
 
inline constexpr reverse_fn reverse{};

[edit] Example

#include <algorithm>
#include <array>
#include <iostream>
#include <string>
 
int main()
{
    std::string s{"ABCDEF"};
    std::cout << s << " → ";
    std::ranges::reverse(s.begin(), s.end());
    std::cout << s << " → ";
    std::ranges::reverse(s);
    std::cout << s << " │ ";
 
    std::array a{1, 2, 3, 4, 5};
    for(auto e : a) { std::cout << e << ' '; }
    std::cout << "→ ";
    std::ranges::reverse(a);
    for(auto e : a) { std::cout << e << ' '; }
    std::cout << '\n';
}

Output:

ABCDEF → FEDCBA → ABCDEF │ 1 2 3 4 5 → 5 4 3 2 1

[edit] See also

creates a copy of a range that is reversed
(niebloid) [edit]
reverses the order of elements in a range
(function template) [edit]