std::rotate_copy
来自 cppreference.cn
定义于头文件 <algorithm> |
||
template< class ForwardIt, class OutputIt > OutputIt rotate_copy( ForwardIt first, ForwardIt middle, |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > |
(2) | (since C++17) |
将 [
first,
last)
的左旋转复制到 d_first。
1) 复制范围
[
first,
last)
中的元素,使得在以 d_first 开头的目标范围中,[
first,
middle)
中的元素放置在 [
middle,
last)
中的元素之后,同时保留两个范围中元素的顺序。2) 与 (1) 相同,但根据 policy 执行。
仅当满足以下所有条件时,此重载才参与重载解析
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(直到 C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(自 C++20 起) |
如果满足以下任何条件,则行为未定义
-
[
first,
middle)
或[
middle,
last)
不是 有效范围。 - 源范围和目标范围重叠。
目录 |
[编辑] 参数
first, last | - | 定义要复制的源范围的迭代器对 |
middle | - | 指向 [ first, last) 中元素的迭代器,该元素应出现在新范围的开头 |
d_first | - | 目标范围的开始 |
policy | - | 要使用的执行策略 |
类型要求 | ||
-ForwardIt, ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。 | ||
-OutputIt 必须满足 LegacyOutputIterator 的要求。 |
[编辑] 返回值
指向复制的最后一个元素之后元素的输出迭代器。
[编辑] 复杂度
正好 std::distance(first, last) 次赋值。
[编辑] 异常
带有名为 ExecutionPolicy
的模板参数的重载报告错误如下
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。 对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 可能的实现
另请参阅 libstdc++、libc++ 和 MSVC STL 中的实现。
[编辑] 示例
运行此代码
#include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> src{1, 2, 3, 4, 5}; std::vector<int> dest(src.size()); auto pivot = std::find(src.begin(), src.end(), 3); std::rotate_copy(src.begin(), pivot, src.end(), dest.begin()); for (int i : dest) std::cout << i << ' '; std::cout << '\n'; // copy the rotation result directly to the std::cout pivot = std::find(dest.begin(), dest.end(), 1); std::rotate_copy(dest.begin(), pivot, dest.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
输出
3 4 5 1 2 1 2 3 4 5
[编辑] 参见
旋转范围中元素的顺序 (函数模板) | |
(C++20) |
复制和旋转元素范围 (算法函数对象) |