std::shift_left、std::shift_right
ja.cppreference.net から
|
ヘッダ <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であれば、効果はありません。 - そうでなければ、
iが[0,last - first - n)内の各整数について、元々位置first + n + iにあった要素を位置first + iに移動します。
移動は
i の昇順で、0 から始めて実行されます。
3) 範囲の末尾方向へ要素をシフトします。
-
もし
n == 0 || n >= last - firstであれば、効果はありません。 - そうでなければ、
iが[0,last - first - n)内の各整数について、元々位置first + iにあった要素を位置first + n + iに移動します。
2,4) それぞれ (1) および (3) と同じですが、
policy に従って実行され、移動は任意の順序で行われて構いません。 これらのオーバーロードは、
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> が true である場合にのみオーバーロード解決に参加します。元の範囲にあったが新しい範囲にはない要素は、有効だが未規定の状態のままになります。
以下の条件のいずれかが満たされる場合、動作は未定義です:
n >= 0trueが- MoveAssignable でない。
*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); // UB, 例: セグフォルト }
出力例:
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++20) |
|
(C++11) |
範囲の要素を逆順で新しい位置に移動する (関数テンプレート & アルゴリズム関数オブジェクト) |
(C++20) |
|
| 範囲内の要素の順序を回転させる (関数テンプレート & アルゴリズム関数オブジェクト) | |
(C++20) |
|
(C++23)(C++23) |
範囲内の要素をシフトする (アルゴリズム関数オブジェクト) |