std::shift_left, std::shift_right
來自 cppreference.com
定義於標頭檔案 <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) |
以逆序將一個範圍的元素移動到一個新位置 (函式模板) |
旋轉一個範圍中元素的順序 (函式模板) | |
移動一個範圍中的元素 (演算法函式物件) |