Namespaces
Variants

std::ranges:: reverse_copy, std::ranges:: reverse_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:: bidirectional_iterator I, std:: sentinel_for < I > S,

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

reverse_copy ( I first, S last, O result ) ;
(1) (C++20以降)
template < ranges:: bidirectional_range R, std:: weakly_incrementable O >

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

reverse_copy ( R && r, O result ) ;
(2) (C++20以降)
ヘルパー型
template < class I, class O >
using reverse_copy_result = ranges:: in_out_result < I, O > ;
(3) (C++20以降)
1) ソース範囲 [ first , last ) の要素を宛先範囲 [ result , result + N ) にコピーする。ここで N ranges:: distance ( first, last ) であり、新しい範囲の要素が逆順になるようにコピーされる。各整数 i に対して * ( result + N - 1 - i ) = * ( first + i ) の代入を実行するかのように動作する。ソース範囲と宛先範囲が重複する場合、動作は未定義である。
2) (1) と同じですが、 r をソース範囲として使用します。これは ranges:: begin ( r ) first として、 ranges:: end ( r ) last として使用する場合と同様です。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる) です。すなわち:

目次

パラメータ

first, last - コピーする要素のソース 範囲 を定義するイテレータ-センチネルペア
r - コピーする要素のソース範囲
result - コピー先範囲の先頭

戻り値

{ last, result + N } .

計算量

正確に N 個の課題。

注記

実装(例: MSVC STL )は、両方のイテレータ型が contiguous_iterator をモデル化し、同じ値型を持ち、かつ値型が TriviallyCopyable である場合にベクトル化を有効化することがあります。

実装例

実装については MSVC STL および libstdc++ も参照してください。

struct reverse_copy_fn
{
    template<std::bidirectional_iterator I, std::sentinel_for<I> S,
             std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::reverse_copy_result<I, O>
        operator()(I first, S last, O result) const
    {
        auto ret = ranges::next(first, last);
        for (; last != first; *result = *--last, ++result);
        return {std::move(ret), std::move(result)};
    }
    template<ranges::bidirectional_range R, std::weakly_incrementable O>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::reverse_copy_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result));
    }
};
inline constexpr reverse_copy_fn reverse_copy {};

#include <algorithm>
#include <iostream>
#include <string>
int main()
{
    std::string x {"12345"}, y(x.size(), ' ');
    std::cout << x << " → ";
    std::ranges::reverse_copy(x.begin(), x.end(), y.begin());
    std::cout << y << " → ";
    std::ranges::reverse_copy(y, x.begin());
    std::cout << x << '\n';
}

出力:

12345 → 54321 → 12345

関連項目

範囲内の要素の順序を反転する
(アルゴリズム関数オブジェクト)
反転された範囲のコピーを作成する
(関数テンプレート)