[ Web Proxy ]
URL:
Viewing: https://en.cppreference.com/cpp/algorithm/ranges/reverse_copy [Back]  [Original]

std::ranges::reverse_copy, std::ranges::reverse_copy_result - cppreference.com
cppreference.com
Namespaces
Variants

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

From cppreference.com
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
(C++11)    

Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations


 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Fold operations (Helper templates)
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Specialized <memory> algorithms
Return types
 
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>
    reverse_copy( I first, S last, O d_first );
(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>
    reverse_copy( R&& r, O d_first );
(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)

Copies elements from the source range [firstlast) or r to the destination range beginning at d_first in reverse order.

If the source and destination ranges overlap, the behavior is undefined.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

Parameters

first, last - the iterator-sentinel pair defining the source range
r - the source range
d_first - the beginning of the destination range

Return value

A ranges::reverse_copy_result object where:

  • The data member in holds the past-the-end iterator of the source range.
  • The data member out holds the past-the-end iterator of the destination range.

Complexity

Given \(\scriptsize N\)N as ranges::distance(first, last) or ranges::distance(r):

1,2) Exactly \(\scriptsize N\)N assignments.

Notes

Implementations (e.g. MSVC STL) may enable vectorization when the both iterator types model contiguous_iterator and have the same value type, and the value type is TriviallyCopyable.

Possible implementation

See also the implementations in MSVC STL and libstdc++.

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::next(ranges::begin(r), ranges::end(r)),
                       std::move(result));
    }
};

inline constexpr reverse_copy_fn reverse_copy{};

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

See also

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

Web Proxy Viewer  |  New URL  |  Original Page