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 ,则无任何效果。
  • 否则,对于每个在 [ 0 , last - first - n ) 区间内的整数 i ,将原本位于位置 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) 结果范围的起始位置。

复杂度

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); <span

参见

(C++11)
将元素范围移动到新位置
(函数模板)
按逆序将元素范围移动到新位置
(函数模板)
旋转范围内元素的顺序
(函数模板)
移位范围内的元素
(算法函数对象)