std::reverse_copy
受约束算法及范围上的算法 (C++20) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<algorithm>
定义
|
||
(1) | ||
template< class BidirIt, class OutputIt >
OutputIt reverse_copy( BidirIt first, BidirIt last, OutputIt d_first ); |
(C++20 前) | |
template< class BidirIt, class OutputIt >
constexpr OutputIt reverse_copy( BidirIt first, BidirIt last, OutputIt d_first ); |
(C++20 起) | |
template< class ExecutionPolicy,
class BidirIt, class
ForwardIt >
ForwardIt reverse_copy( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
[
first,
last)
中的元素复制到从
d_first 开始的包含 \(\scriptsize N\)N
个元素的新范围(目标范围),使得目标范围中元素以逆序排列。[
first,
last)
和目标范围重叠,那么行为未定义。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> |
(C++20 起) |
参数
first, last | - | 要复制的元素范围 |
d_first | - | 新范围的起始 |
类型要求 | ||
-BidirIt 必须符合老式双向迭代器 (LegacyBidirectionalIterator)
的要求。
|
||
-OutputIt 必须符合老式输出迭代器 (LegacyOutputIterator)
的要求。
|
||
-ForwardIt 必须符合老式向前迭代器 (LegacyForwardIterator)
的要求。
|
返回值
指向最后被复制元素后一元素的迭代器。
复杂度
赋值 \(\scriptsize N\)N 次。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy
,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
注解
实现(例如 MSVC STL )可能在两个迭代器类型均满足老式连续迭代器 (LegacyContiguousIterator) 并拥有同一值类型,且值类型可平凡复制 (TriviallyCopyable) 时启用向量化。
可能的实现
参阅 libstdc++、 libc++ 和 MSVC STL 中的实现。
template<class BidirIt, class OutputIt> constexpr // C++20 起 OutputIt reverse_copy(BidirIt first, BidirIt last, OutputIt d_first) { for (; first != last; ++d_first) *d_first = *(--last); return d_first; } |
示例
#include <algorithm> #include <iostream> #include <vector> int main() { auto print = [](const std::vector<int>& v) { for (const auto& value : v) std::cout << value << ' '; std::cout << '\n'; }; std::vector<int> v{1, 2, 3}; print(v); std::vector<int> destination(3); std::reverse_copy(std::begin(v), std::end(v), std::begin(destination)); print(destination); std::reverse_copy(std::rbegin(v), std::rend(v), std::begin(destination)); print(destination); }
输出:
1 2 3 3 2 1 1 2 3
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2074 | C++98 | 对于每个 i,赋值操作是 *(d_first + N - i) = *(first + i)[1] |
改成 *(d_first + N - 1 - i) = *(first + i)[1] |
- ↑ 1.0 1.1 1.2 老式输出迭代器
(LegacyOutputIterator)
不需要支持二元
+
和-
。这里使用+
和-
仅用于阐述:实际运算不需要用到它们。
参阅
逆转范围中的元素顺序 (函数模板) |
|
(C++20)
|
创建一个范围的逆向副本 (niebloid) |