名稱空間
變體
操作

std::shift_left, std::shift_right

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <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 起)

將範圍 [firstlast) 中的元素移動 n 個位置。

1) 將元素向範圍的起始處移動。
  • 如果 n == 0 || n >= last - first,則沒有效果。
  • 否則,對於 [0last - first - n) 中的每個整數 i,將原本位於位置 first + n + i 的元素移動到位置 first + i
移動按 i0 開始遞增的順序執行。
3) 將元素向範圍的末尾移動。
  • 如果 n == 0 || n >= last - first,則沒有效果。
  • 否則,對於 [0last - first - n) 中的每個整數 i,將原本位於位置 first + i 的元素移動到位置 first + n + i
如果 ForwardIt 滿足 LegacyBidirectionalIterator 要求,則移動按 ilast - 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_leftstd::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)
將一個範圍的元素移動到一個新位置
(函式模板) [編輯]
以逆序將一個範圍的元素移動到一個新位置
(函式模板) [編輯]
旋轉一個範圍中元素的順序
(函式模板) [編輯]
移動一個範圍中的元素
(演算法函式物件)[編輯]