Namespaces
Variants

std:: rotate_copy

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
定义于头文件 <algorithm>
template < class ForwardIt, class OutputIt >

OutputIt rotate_copy ( ForwardIt first, ForwardIt middle,

ForwardIt last, OutputIt d_first ) ;
(1) (自 C++20 起为 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2 >
ForwardIt2 rotate_copy ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 middle,

ForwardIt1 last, ForwardIt2 d_first ) ;
(2) (自 C++17 起)

[ first , last ) 左旋转 复制到 d_first

1) 复制范围 [ first , last ) 中的元素,使得在起始于 d_first 的目标范围中, [ first , middle ) 中的元素被放置在 [ middle , last ) 中的元素之后,同时保持两个范围内元素的原有顺序。
2) (1) 相同,但根据 policy 执行。
此重载仅在满足以下所有条件时参与重载决议:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true

(C++20 前)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true

(C++20 起)

若满足以下任一条件,则行为未定义:

  • [ first , middle ) [ middle , last ) 不是有效的 范围
  • 源范围与目标范围存在重叠。

目录

参数

first, last - 定义待复制元素源 范围 的迭代器对
middle - 指向 [ first , last ) 范围内应出现在新范围起始位置的元素的迭代器
d_first - 目标范围的起始位置
policy - 使用的 执行策略
类型要求
-
ForwardIt, ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。
-
OutputIt 必须满足 LegacyOutputIterator 的要求。

返回值

指向最后一个被复制元素之后位置的输出迭代器。

复杂度

恰好进行 std:: distance ( first, last ) 次赋值操作。

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

  • 如果作为算法一部分调用的函数执行抛出异常,且 ExecutionPolicy 是某个 标准策略 ,则调用 std::terminate 。对于其他任何 ExecutionPolicy ,其行为由实现定义。
  • 如果算法无法分配内存,则抛出 std::bad_alloc

可能的实现

另请参阅以下实现: libstdc++ libc++ MSVC STL

template<class ForwardIt, class OutputIt>
constexpr // 自 C++20 起
OutputIt rotate_copy(ForwardIt first, ForwardIt middle,
                     ForwardIt last, OutputIt d_first)
{
    d_first = std::copy(middle, last, d_first);
    return std::copy(first, middle, d_first);
}

示例

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
    std::vector<int> src{1, 2, 3, 4, 5};
    std::vector<int> dest(src.size());
    auto pivot = std::find(src.begin(), src.end(), 3);
    std::rotate_copy(src.begin(), pivot, src.end(), dest.begin());
    for (int i : dest)
        std::cout << i << ' ';
    std::cout << '\n';
    // 将旋转结果直接复制到 std::cout
    pivot = std::find(dest.begin(), dest.end(), 1);
    std::rotate_copy(dest.begin(), pivot, dest.end(),
                     std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

输出:

3 4 5 1 2
1 2 3 4 5

参见

旋转范围内元素的顺序
(函数模板)
复制并旋转元素范围
(算法函数对象)