Namespaces
Variants
Views
Actions

std::ranges::reverse_copy, std::ranges::reverse_copy_result

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,

         std::weakly_incrementable O>
  requires std::indirectly_copyable<I, O>
    constexpr ranges::reverse_copy_result<I, O>

      ranges::reverse_copy( I first, S last, O result );
(1) (since C++20)
template<ranges::bidirectional_range R, std::weakly_incrementable O>

  requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::reverse_copy_result<ranges::borrowed_iterator_t<R>, O>

      ranges::reverse_copy( R&& r, O result );
(2) (since C++20)
Helper types
template<class I, class O>
  using reverse_copy_result = ranges::in_out_result<I, O>;
(3) (since C++20)
1) Copies the elements from the source range [first, last) to the destination range [result, result + N), where N = last - first, in such a way that the elements in the new range are in reverse order.
Behaves as if by executing the assignment *(result + N - 1 - i) = *(first + i) once for each integer 0 ≤ i < N.
Preconditions: the source and destination ranges do not overlap.
2) Same as (1), but uses r as the source 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 copy
r - the range of elements to copy
result - the beginning of the destination range.

[edit] Return value

An object equal to {last, result + N}.

[edit] Complexity

Exactly N assignments.

[edit] Possible implementation

struct reverse_copy_fn {
  template<std::bidirectional_iterator I, std::sentinel_for<I> S,
           std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
      constexpr ranges::reverse_copy_result<I, O>
        operator() ( I first, S last, O result ) const {
            auto ret = ranges::next(first, last);
            for (; last != first; *result = *--last, ++result);
            return {std::move(ret), std::move(result)};
        }
 
  template<ranges::bidirectional_range R, std::weakly_incrementable O>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
      constexpr ranges::reverse_copy_result<ranges::borrowed_iterator_t<R>, O>
        operator() ( R&& r, O result ) const {
          return (*this)(ranges::begin(r), ranges::end(r), std::move(result));
        }
};
 
inline constexpr reverse_copy_fn reverse_copy{};

[edit] Example

#include <algorithm>
#include <iostream>
#include <string>
 
int main()
{
    std::string x{"12345"}, y(x.size(), ' ');
    std::cout << x << " → ";
    std::ranges::reverse_copy(x.begin(), x.end(), y.begin());
    std::cout << y << " → ";
    std::ranges::reverse_copy(y, x.begin());
    std::cout << x << '\n';
}

Output:

12345 → 54321 → 12345

[edit] See also

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