Namespaces
Variants

std:: shift_left, std:: shift_right

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
shift_left shift_right
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダー <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 の場合、効果はありません。
  • それ以外の場合、すべての整数 i について [ 0 , last - first - n ) の範囲で、元々位置 first + i にあった要素を位置 first + n + i に移動します。
ForwardIt LegacyBidirectionalIterator 要件を満たす場合、移動操作は i の降順で実行され、 last - first - n - 1 から開始されます。
2,4) (1) および (3) とそれぞれ同じですが、 policy に従って実行され、操作は任意の順序で実行される可能性があります。
これらのオーバーロードは、以下の条件が満たされる場合にのみオーバーロード解決に参加します: std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true である場合。

元の範囲にあったが新しい範囲に含まれない要素は、有効だが未指定の状態のまま残されます。

以下のいずれかの条件が満たされる場合、動作は未定義です:

目次

パラメータ

first, last - 要素をシフトする範囲を定義するイテレータのペア
n - シフトする位置の数
policy - 使用する実行ポリシー
型要件
-
ForwardIt LegacyForwardIterator の要件を満たさなければならない。

戻り値

1,2) 結果範囲の終端。
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)
要素の範囲を新しい位置に移動する
(関数テンプレート)
要素の範囲を逆順で新しい位置に移動する
(関数テンプレート)
範囲内の要素の順序を回転させる
(関数テンプレート)
範囲内の要素をシフトする
(アルゴリズム関数オブジェクト)