Namespaces
Variants

std::ranges:: copy, std::ranges:: copy_if, std::ranges:: copy_result, std::ranges:: copy_if_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
(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
定義済みヘッダー <algorithm>
呼び出しシグネチャ
template < std:: input_iterator I, std:: sentinel_for < I > S, std:: weakly_incrementable O >

requires std:: indirectly_copyable < I, O >
constexpr copy_result < I, O >

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

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

copy ( R && r, O result ) ;
(2) (C++20以降)
template < std:: input_iterator I, std:: sentinel_for < I > S, std:: weakly_incrementable O,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >
requires std:: indirectly_copyable < I, O >
constexpr copy_if_result < I, O >

copy_if ( I first, S last, O result, Pred pred, Proj proj = { } ) ;
(3) (C++20以降)
template < ranges:: input_range R, std:: weakly_incrementable O,

class Proj = std:: identity ,
std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > , Proj >> Pred >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O >
constexpr copy_if_result < ranges:: borrowed_iterator_t < R > , O >

copy_if ( R && r, O result, Pred pred, Proj proj = { } ) ;
(4) (C++20以降)
ヘルパー型
template < class I, class O >
using copy_result = ranges:: in_out_result < I, O > ;
(5) (C++20以降)
template < class I, class O >
using copy_if_result = ranges:: in_out_result < I, O > ;
(6) (C++20以降)

[ first , last ) で定義される範囲の要素を、 result を先頭とする別の範囲にコピーします。

1) 範囲 [ first , last ) 内のすべての要素を first から last - 1 まで順にコピーします。 result が範囲 [ first , last ) 内にある場合の動作は未定義です。この場合、 ranges::copy_backward の使用を検討してください。
3) 述語 pred true を返す要素のみをコピーします。コピーされる要素の相対的な順序は保持されます。ソースとデスティネーションの範囲が重なっている場合の動作は未定義です。
2,4) (1,3) と同じですが、 r をソース範囲として使用します。これは ranges:: begin ( r ) first として、 ranges:: end ( r ) last として使用する場合と同様です。

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

目次

パラメータ

first, last - コピーする要素の 範囲 を定義するイテレータ-センチネルペア
r - コピーする要素の範囲
result - コピー先範囲の先頭
pred - 投影された要素に適用する述語
proj - 要素に適用する投影

戻り値

ranges::in_out_result を含む、入力イテレータが last と等しく、最後にコピーされた要素を超える出力イテレータを持つ結果。

計算量

1,2) 正確に last - first 回の代入が行われる。
3,4) 厳密に last - first 回の述語と射影の適用、および 0 から last - first 回の代入(述語が true を返すすべての要素に対して代入が実行される。これは述語と入力データに依存する)。

注記

実際には、 ranges::copy の実装は複数の代入を回避し、値型が TriviallyCopyable であり、イテレータ型が contiguous_iterator を満たす場合、 std::memmove のようなバルクコピー関数を使用します。

オーバーラップする範囲をコピーする場合、 ranges::copy は左方向へのコピー(コピー先範囲の先頭がコピー元範囲の外側にある場合)に適しており、 ranges::copy_backward は右方向へのコピー(コピー先範囲の終端がコピー元範囲の外側にある場合)に適しています。

実装例

HTMLタグや属性は翻訳せず、元のフォーマットを保持します。` `, `
`, ``タグ内のテキストは翻訳しません。C++固有の用語は翻訳しません。正確性と専門性が求められます。
**注記**: 提供されたHTMLコードはC++のコードスニペットを含んでおり、指示に従って以下の点を厳密に守りました: - HTMLタグと属性は一切翻訳せず - ` `, `
`, ``タグ内のテキストは翻訳せず
- C++固有の用語(`struct`, `template`, `constexpr`, `operator`など)は翻訳せず
コード部分は完全に元のまま保持されています。
**注記**: 提供されたHTMLコードはC++のコードスニペットを含んでおり、指示に従って以下の点を厳密に守りました: - HTMLタグと属性は一切翻訳せず - ` `, `
`, ``タグ内のテキストは翻訳せず
- C++固有の用語(`struct`, `template`, `constexpr`など)は翻訳せず
- 元のフォーマットを完全に保持
コード部分は全て原文のまま保持されています。
copy (1)(2)
struct copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::copy_result<I, O> operator()(I first, S last, O result) const
    {
        for (; first != last; ++first, (void)++result)
            *result = *first;
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, std::weakly_incrementable O>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::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 copy_fn copy;
copy_if (3)(4)
struct copy_if_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::copy_if_result<I, O>
        operator()(I first, S last, O result, Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (std::invoke(pred, std::invoke(proj, *first)))
            {
                *result = *first;
                ++result;
            }
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>, Proj>> Pred>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::copy_if_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
                       std::ref(pred), std::ref(proj));
    }
};
inline constexpr copy_if_fn copy_if;

以下のコードは ranges::copy を使用して、一方の std::vector の内容を別の std::vector にコピーし、結果の std::vector を表示します。

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
    std::vector<int> source(10);
    std::iota(source.begin(), source.end(), 0);
    std::vector<int> destination;
    std::ranges::copy(source.begin(), source.end(), std::back_inserter(destination));
// または、代替案として
//  std::vector<int> destination(source.size());
//  std::ranges::copy(source.begin(), source.end(), destination.begin());
// どちらの方法も以下と等価
//  std::vector<int> destination = source;
    std::cout << "Destination contains: ";
    std::ranges::copy(destination, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    std::cout << "Odd numbers in destination are: ";
    std::ranges::copy_if(destination, std::ostream_iterator<int>(std::cout, " "),
                         [](int x) { return (x % 2) == 1; });
    std::cout << '\n';
}

出力:

Destination contains: 0 1 2 3 4 5 6 7 8 9
Odd numbers in destination are: 1 3 5 7 9

関連項目

要素の範囲を逆順にコピーする
(アルゴリズム関数オブジェクト)
反転された範囲のコピーを作成する
(アルゴリズム関数オブジェクト)
指定された数の要素を新しい位置にコピーする
(アルゴリズム関数オブジェクト)
要素の範囲に特定の値を代入する
(アルゴリズム関数オブジェクト)
特定の条件を満たす要素を除外して範囲をコピーする
(アルゴリズム関数オブジェクト)
要素の範囲を新しい位置にコピーする
(関数テンプレート)