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 ,则行为未定义。
  • 否则,对于每个在 [ 0 , last - first - n ) 区间内的整数 i ,将原位于 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</

参见

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