std::ranges::shift_left, std::ranges::shift_right
来自 zh.cppreference.net
| 定义于头文件 <algorithm>
|
||
| 调用签名 |
||
template< std::permutable I, std::sentinel_for<I> S >
constexpr ranges::subrange<I>
shift_left( I first, S last, std::iter_difference_t<I> n );
|
(1) | (自 C++23 起) |
template< ranges::forward_range R >
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>
shift_left( R&& r, ranges::range_difference_t<R> n );
|
(2) | (自 C++23 起) |
template< std::permutable I, std::sentinel_for<I> S >
constexpr ranges::subrange<I>
shift_right( I first, S last, std::iter_difference_t<I> n );
|
(3) | (自 C++23 起) |
template< ranges::forward_range R >
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>
shift_right( R&& r, ranges::range_difference_t<R> n );
|
(4) | (自 C++23 起) |
将范围 [first, last) 或 r 中的元素平移 n 个位置。如果 [first, last) 不是有效范围,则行为未定义。
1) 将元素向范围起始方向平移:
- 若
n == 0 || n >= last - first,则无效果。 - 若
n < 0,则行为未定义。 - 否则,对于
i中的每个整数[0,last - first - n),将原位于位置first + n + i的元素移动到位置first + i。移动按i递增的顺序执行,从0开始。
3) 将元素向范围末尾方向平移:
- 若
n == 0 || n >= last - first,则无效果。 - 若
n < 0,则行为未定义。 - 否则,对于
i中的每个整数[0,last - first - n),将原位于位置first + i的元素移动到位置first + n + i。若I满足 bidirectional_iterator,则移动按i递减的顺序执行,从last - first - n - 1开始。
2,4) 分别与 (1) 或 (3) 相同,但使用
r 作为范围,如同使用 ranges::begin(r) 作为 first、ranges::end(r) 作为 last 一样。存在于原范围但不在新范围中的元素将保持有效但未指定的状态。
此页面上描述的类似函数的实体是 算法函数对象(非正式地称为 niebloids),这意味着:
参数
| first, last | - | 定义待移位元素范围的 区间 的迭代器-哨位对 |
| r | - | 待移位元素的区间 |
| n | - | 需要移位的位数 |
返回值
1,2)
{
first,
/*NEW_LAST*/
}
,其中
NEW_LAST
表示结果范围的末尾,等价于:
-
first
+
(
last
-
first
-
n
)
(若
n小于 last - first ); - 否则为 first 。
3,4)
{
/*NEW_FIRST*/
, last
}
,其中
NEW_FIRST
表示结果范围的起始位置,其值等于:
-
first
+
n
(当
n小于 last - first 时); - last (其他情况)。
复杂度
1,2)
最多进行
ranges::
distance
(
first, last
)
-
n
次赋值操作。
3,4)
最多进行
ranges::
distance
(
first, last
)
-
n
次赋值或交换。
注释
ranges::shift_left
/
ranges::shift_right
在常见实现中具有更高效率,若
I
满足
bidirectional_iterator
或(更优)
random_access_iterator
要求。
实现(例如
MSVC STL
)可在迭代器类型满足
contiguous_iterator
概念且交换其值类型时既不调用非平凡特殊成员函数、也不调用通过
ADL
查找到的
swap
时启用向量化优化。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_shift
|
202202L
|
(C++23) |
std::ranges::shift_left
和
std::ranges::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::ranges::shift_left(a, 3); std::ranges::shift_left(b, 3); std::ranges::shift_left(c, 3); std::cout << a << " " << b << " " << c << '\n'; std::ranges::shift_right(a, 2); std::ranges::shift_right(b, 2); std::ranges::shift_right(c, 2); std::cout << a << " " << b << " " << c << '\n'; std::ranges::shift_left(a, 8); // 无效果:n >= last - first std::ranges::shift_left(b, 8); // 同上 std::ranges::shift_left(c, 8); // 同上 std::cout</
参阅
(C++20) |
移动元素范围到新位置 (算法函数对象) |
(C++20) |
按逆序移动元素范围到新位置 (算法函数对象) |
(C++20) |
旋转范围内的元素顺序 (算法函数对象) |
(C++20)(C++20) |
移位元素范围 (函数模板) |