Namespaces
Variants

std::ranges:: rotate_copy, std::ranges:: rotate_copy_result

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:: forward_iterator I, std:: sentinel_for < I > S,

std:: weakly_incrementable O >
requires std:: indirectly_copyable < I, O >
constexpr rotate_copy_result < I, O >

rotate_copy ( I first, I middle, S last, O result ) ;
(1) (C++20 起)
template < ranges:: forward_range R, std:: weakly_incrementable O >

requires std:: indirectly_copyable < ranges:: iterator_t < R > , O >
constexpr rotate_copy_result < ranges:: borrowed_iterator_t < R > , O >

rotate_copy ( R && r, ranges:: iterator_t < R > middle, O result ) ;
(2) (C++20 起)
辅助类型
template < class I, class O >
using rotate_copy_result = in_out_result < I, O > ;
(3) (C++20 起)

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

1) 复制源范围 [ first , last ) 中的元素,使得在目标范围中, [ first , middle ) 区间的元素被放置在 [ middle , last ) 区间元素之后,同时保持两个区间内元素的原有顺序。
如果 [ first , middle ) [ middle , last ) 不是有效范围,或源范围与目标范围重叠,则行为未定义。
2) (1) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first ,以及 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待复制元素源 范围 的迭代器-哨位对
r - 待复制元素的源范围
middle - 指向应出现在目标范围起始位置的元素的迭代器
result - 目标范围的起始位置

返回值

{ last, result + N } ,其中 N = ranges:: distance ( first, last )

复杂度

线性 :精确执行 N 次赋值操作。

注释

若值类型满足 可平凡复制 且迭代器类型满足 contiguous_iterator ranges::rotate_copy 的实现通常通过使用 std::memmove 等“批量拷贝”函数来避免多次赋值操作。

可能的实现

另请参阅 libstdc++ MSVC STL 中的实现。

struct rotate_copy_fn
{
    template<std::forward_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::rotate_copy_result<I, O>
        operator()(I first, I middle, S last, O result) const
    {
        auto c1 {ranges::copy(middle, std::move(last), std::move(result))};
        auto c2 {ranges::copy(std::move(first), std::move(middle), std::move(c1.out))};
        return {std::move(c1.in), std::move(c2.out)};
    }
    template<ranges::forward_range R, std::weakly_incrementable O>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::rotate_copy_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, ranges::iterator_t<R> middle, O result) const
    {
        return (*this)(ranges::begin(r), std::move(middle),
                       ranges::end(r), std::move(result));
    }
};
inline constexpr rotate_copy_fn rotate_copy {};

示例

#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::ranges::find(src, 3);
    std::ranges::rotate_copy(src, pivot, dest.begin());
    for (int i : dest)
        std::cout << i << ' ';
    std::cout << '\n';
    // 将旋转结果直接复制到 std::cout
    pivot = std::ranges::find(dest, 1);
    std::ranges::rotate_copy(dest, pivot, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

输出:

3 4 5 1 2
1 2 3 4 5

参见

旋转范围中元素的顺序
(算法函数对象)
将元素范围复制到新位置
(算法函数对象)
复制并旋转元素范围
(函数模板)