定义于头文件 <algorithm>
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last)
,其中 last
指代要查询或修改的最后元素的后一个元素。
旋转范围中的元素顺序
std::rotate
template< class ForwardIt > void rotate( ForwardIt first, ForwardIt n_first, ForwardIt last ); |
(1) |
(C++11 前) |
template< class ForwardIt > ForwardIt rotate( ForwardIt first, ForwardIt n_first, ForwardIt last ); |
(C++11 起) (C++20 前) |
template< class ForwardIt > constexpr ForwardIt rotate( ForwardIt first, ForwardIt n_first, ForwardIt last ); |
(C++20 起) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt rotate( ExecutionPolicy&& policy, ForwardIt first, ForwardIt n_first, ForwardIt last ); |
(2) |
(C++17 起) |
1) 进行元素范围上的左旋转。
具体而言, std::rotate
交换范围 [first, last)
中的元素,方式满足元素 n_first
成为新范围的首个元素,而 n_first - 1
成为最后元素。
此函数的前提条件是 [first, n_first)
和 [n_first, last)
为合法范围。
2) 同 (1) ,但按照 policy
执行。这些重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true
才参与重载决议。
参数
first |
- |
原范围的起始 |
n_first |
- |
应出现在旋转后范围起始的元素 |
last |
- |
原范围的结尾 |
policy |
- |
所用的执行策略。细节见执行策略。 |
类型要求 |
- ForwardIt 必须满足值可交换 (ValueSwappable) 和 遗留向前迭代器 (LegacyForwardIterator) 的要求。 |
- 解引用 ForwardIt 结果的类型必须满足可移动赋值 (MoveAssignable) 和可移动构造 (MoveConstructible) 的要求。 |
返回值
(无) |
(C++11 前) |
指向 first 指向的元素所在的新位置的迭代器。等于 first + (last - n_first) 。 |
(C++11 起) |
复杂度
与 first
和 last
间的距离成线性。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 若作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy
,行为是实现定义的。
- 若算法无法分配内存,则抛出 std::bad_alloc 。
可能的实现
template<class ForwardIt>
ForwardIt rotate(ForwardIt first, ForwardIt n_first, ForwardIt last)
{
if(first == n_first) return last;
if(n_first == last) return first;
auto read = n_first;
auto write = first;
auto next_read = first; // "read" 撞击 "last" 时的读取位置
while(read != last) {
if(write == next_read) next_read = read; // 跟踪 "first" 所至
std::iter_swap(write++, read++);
}
// 旋转剩余序列到位置中
(rotate)(write, next_read, last);
return write;
}
调用示例
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iterator>
using namespace std;
struct Cell
{
int x;
int y;
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
int main()
{
auto func1 = [](Cell & cell, const Cell & t)
{
cell += t;
return cell;
};
Cell cell{99, 100};
Cell t{2, 3};
auto func2 = std::bind(func1, cell, t);
vector<Cell> cells(8);
std::generate(cells.begin(), cells.end(), func2);
std::cout << "original : ";
std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::rotate(cells.begin(), cells.begin() + cells.size() / 3, cells.end());
std::cout << "simple rotate left : ";
std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::rotate(cells.rbegin(), cells.rbegin() + cells.size() / 3, cells.rend());
std::cout << "simple rotate right : ";
std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::rotate(cells.rbegin(), cells.rbegin() + cells.size() / 3, cells.rend());
std::cout << "simple rotate right : ";
std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
return 0;
}
输出