std::shift_left, std::shift_right
来自 cppreference.cn
定义于头文件 <algorithm> |
||
template< class ForwardIt > constexpr ForwardIt shift_left( ForwardIt first, ForwardIt last, |
(1) | (C++20 起) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt shift_left( ExecutionPolicy&& policy, |
(2) | (C++20 起) |
template< class ForwardIt > constexpr ForwardIt shift_right( ForwardIt first, ForwardIt last, |
(3) | (C++20 起) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt shift_right( ExecutionPolicy&& policy, |
(4) | (C++20 起) |
将范围 [
first,
last)
中的元素移动 n 个位置。
1) 将元素向范围的起始处移动。
- 如果 n == 0 || n >= last - first,则没有效果。
- 否则,对于
[
0,
last - first - n)
中的每个整数 i,将原本位于位置 first + n + i 的元素移动到位置 first + i。
移动按
i
从 0 开始递增的顺序执行。3) 将元素向范围的末尾移动。
- 如果 n == 0 || n >= last - first,则没有效果。
- 否则,对于
[
0,
last - first - n)
中的每个整数 i,将原本位于位置 first + i 的元素移动到位置 first + n + i。
2,4) 分别与 (1) 和 (3) 相同,但根据 policy 执行,并且移动可以按任何顺序执行。
仅当 std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true 时,这些重载才参与重载决议。
在原始范围中但不在新范围中的元素保持有效但未指定的状态。
如果满足以下任何条件,则行为是未定义的:
- n >= 0 不为 true。
- *first 的类型不是 可移动赋值 (MoveAssignable) 的。
- 对于
shift_right
,ForwardIt
既不是 LegacyBidirectionalIterator 也不是 ValueSwappable。
目录 |
[编辑] 参数
first, last | - | 定义要移动元素的范围的迭代器对 |
n | - | 移动的位置数 |
policy | - | 要使用的 执行策略 |
类型要求 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 |
[编辑] 返回值
1,2) 结果范围的末尾。
- 如果 n 小于 std::distance(first, last),则返回等于 std::next(first, (std::distance(first, last) - n)) 的迭代器。
- 否则,返回 first。
3,4) 结果范围的起始。
- 如果 n 小于 std::distance(first, last),则返回等于 std::next(first, n) 的迭代器。
- 否则,返回 last。
[编辑] 复杂度
1,2) 最多 std::distance(first, last) - n 次赋值。
3,4) 最多 std::distance(first, last) - n 次赋值或交换。
[编辑] 异常
带有模板参数 ExecutionPolicy
的重载按如下方式报告错误
- 如果作为算法一部分调用的函数执行时抛出异常且
ExecutionPolicy
为 标准策略 之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法未能分配内存,则抛出 std::bad_alloc。
[编辑] 注意
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_shift |
201806L |
(C++20) | std::shift_left 和 std::shift_right |
[编辑] 示例
运行此代码
#include <algorithm> #include <iostream> #include <string> #include <type_traits> #include <vector> struct S { int value{0}; bool specified_state{true}; S(int v = 0) : value{v} {} S(S const& rhs) = default; S(S&& rhs) { *this = std::move(rhs); } S& operator=(S const& rhs) = default; S& operator=(S&& rhs) { if (this != &rhs) { value = rhs.value; specified_state = rhs.specified_state; rhs.specified_state = false; } return *this; } }; template<typename T> std::ostream& operator<<(std::ostream& os, std::vector<T> const& v) { for (const auto& s : v) { if constexpr (std::is_same_v<T, S>) s.specified_state ? os << s.value << ' ' : os << ". "; else if constexpr (std::is_same_v<T, std::string>) os << (s.empty() ? "." : s) << ' '; else os << s << ' '; } return os; } int main() { std::cout << std::left; std::vector<S> a{1, 2, 3, 4, 5, 6, 7}; std::vector<int> b{1, 2, 3, 4, 5, 6, 7}; std::vector<std::string> c{"α", "β", "γ", "δ", "ε", "ζ", "η"}; std::cout << "vector<S> \tvector<int> \tvector<string>\n"; std::cout << a << " " << b << " " << c << '\n'; std::shift_left(begin(a), end(a), 3); std::shift_left(begin(b), end(b), 3); std::shift_left(begin(c), end(c), 3); std::cout << a << " " << b << " " << c << '\n'; std::shift_right(begin(a), end(a), 2); std::shift_right(begin(b), end(b), 2); std::shift_right(begin(c), end(c), 2); std::cout << a << " " << b << " " << c << '\n'; std::shift_left(begin(a), end(a), 8); // has no effect: n >= last - first std::shift_left(begin(b), end(b), 8); // ditto std::shift_left(begin(c), end(c), 8); // ditto std::cout << a << " " << b << " " << c << '\n'; // std::shift_left(begin(a), end(a), -3); // UB, e.g. segfault }
可能的输出
vector<S> vector<int> vector<string> 1 2 3 4 5 6 7 1 2 3 4 5 6 7 α β γ δ ε ζ η 4 5 6 7 . . . 4 5 6 7 5 6 7 δ ε ζ η . . . . . 4 5 6 7 . 4 5 4 5 6 7 5 . . δ ε ζ η . . . 4 5 6 7 . 4 5 4 5 6 7 5 . . δ ε ζ η .
[编辑] 参阅
(C++11) |
将一个范围的元素移动到一个新位置 (函数模板) |
(C++11) |
以逆序将一个范围的元素移动到一个新位置 (函数模板) |
旋转一个范围中元素的顺序 (函数模板) | |
移动一个范围中的元素 (算法函数对象) |