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 ) から要素をコピーし、宛先範囲において [ middle , last ) の要素の後に [ first , middle ) の要素が配置されるようにする。両範囲の要素の順序は保持される。
以下のいずれかの場合、動作は未定義である: [ 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 ) です。

計算量

Linear : 正確に N 回の代入。

注記

値型が TriviallyCopyable であり、イテレータ型が 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

関連項目

範囲内の要素の順序を回転させる
(アルゴリズム関数オブジェクト)
要素の範囲を新しい位置にコピーする
(アルゴリズム関数オブジェクト)
要素の範囲をコピーして回転させる
(関数テンプレート)