std::shift_left, std::shift_right
来自cppreference.com
<tbody>
</tbody>
| 在标头 <algorithm> 定义
|
||
template< class ForwardIt > constexpr ForwardIt shift_left( ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>:: difference_type n ); |
(1) | (C++20 起) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt shift_left( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>:: difference_type n ); |
(2) | (C++20 起) |
template< class ForwardIt > constexpr ForwardIt shift_right( ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>:: difference_type n ); |
(3) | (C++20 起) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt shift_right( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>:: difference_type n ); |
(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::decay_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); // 无效果:n >= last - first
std::shift_left(begin(b), end(b), 8); // 同上
std::shift_left(begin(c), end(c), 8); // 同上
std::cout << a << " " << b << " " << c << '\n';
// std::shift_left(begin(a), end(a),-3); // 未定义行为,例如段错误
}
可能的输出:
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) |
从后往前将范围中元素移到新位置 (函数模板) |
| 旋转范围中的元素顺序 (函数模板) | |
| 迁移范围中元素 (算法函数对象) |