std:: shift_left, std:: shift_right
|
ヘッダー
<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
位置分シフトします。
- n == 0 || n >= last - first の場合、効果はありません。
-
それ以外の場合、
[ 0 ,last - first - n)の範囲内のすべての整数 i について、元々位置 first + n + i にあった要素を位置 first + i に移動します。
i
の昇順で実行され、
0
から始まります。
- n == 0 || n >= last - first の場合、効果はありません。
-
それ以外の場合、すべての整数
i
について
[ 0 ,last - first - n)の範囲で、元々位置 first + i にあった要素を位置 first + n + i に移動します。
元の範囲にあったが新しい範囲に含まれない要素は、有効だが未指定の状態のまま残されます。
以下のいずれかの条件が満たされる場合、動作は未定義です:
- n >= 0 が真ではない場合。
- * first の型が MoveAssignable ではない場合。
-
shift_rightにおいて、ForwardItが LegacyBidirectionalIterator でも ValueSwappable でもない場合。
目次 |
パラメータ
| first, last | - | 要素をシフトする範囲を定義するイテレータのペア |
| n | - | シフトする位置の数 |
| policy | - | 使用する実行ポリシー |
| 型要件 | ||
-
ForwardIt
は
LegacyForwardIterator
の要件を満たさなければならない。
|
||
戻り値
- n が std:: distance ( first, last ) より小さい場合、 std:: next ( first, ( std:: distance ( first, last ) - n ) ) と等しいイテレータを返す。
- それ以外の場合、 first を返す。
- n が std:: distance ( first, last ) より小さい場合、 std:: next ( first, n ) に等しいイテレータを返す。
- それ以外の場合、 last を返す。
計算量
例外
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++11)
|
要素の範囲を逆順で新しい位置に移動する
(関数テンプレート) |
|
範囲内の要素の順序を回転させる
(関数テンプレート) |
|
|
範囲内の要素をシフトする
(アルゴリズム関数オブジェクト) |