Namespaces
Variants

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

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
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
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
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
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
template < std:: bidirectional_iterator I, std:: sentinel_for < I > S,

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

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

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

reverse_copy ( R && r, O result ) ;
(2) (C++20 起)
辅助类型
template < class I, class O >
using reverse_copy_result = ranges:: in_out_result < I, O > ;
(3) (C++20 起)
1) 将源范围 [ first , last ) 中的元素复制到目标范围 [ result , result + N ) ,其中 N ranges:: distance ( first, last ) ,且新范围中的元素呈逆序排列。其行为相当于对区间 [ 0 , N ) 内的每个整数 i 执行一次赋值操作 * ( result + N - 1 - i ) = * ( first + i ) 。若源范围与目标范围存在重叠,则行为未定义。
2) (1) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first ,以及 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待复制元素源 范围 的迭代器-哨位对
r - 待复制元素的源范围
result - 目标范围的起始位置

返回值

{ last, result + N }

复杂度

恰好有 N 个任务。

注释

实现(例如 MSVC STL )可在以下情况启用向量化:当两个迭代器类型均建模 contiguous_iterator 且具有相同值类型,同时该值类型满足 TriviallyCopyable 要求时。

可能的实现

另请参阅 MSVC STL 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::end(r), std::move(result));
    }
};
inline constexpr reverse_copy_fn reverse_copy {};

示例

#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';
}

输出:

12345 → 54321 → 12345

参见

反转范围中元素的顺序
(算法函数对象)
创建反转后的范围副本
(函数模板)