Namespaces
Variants

std::ranges:: shift_left, std::ranges:: 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)
(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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
ヘッダーで定義 <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以降)
(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以降)
(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 の場合、動作は未定義。
  • それ以外の場合、区間 [ 0 , last - first - n ) 内の全ての整数 i について、元々位置 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 << a << "  " << b << "  " << c << '\n';
//  std::ranges::shift_left(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   . . δ ε ζ η .

関連項目

要素の範囲を新しい位置に移動する
(アルゴリズム関数オブジェクト)
要素の範囲を逆順で新しい位置に移動する
(アルゴリズム関数オブジェクト)
範囲内の要素の順序を回転させる
(アルゴリズム関数オブジェクト)
範囲内の要素をシフトする
(関数テンプレート)