Namespaces
Variants

std:: copy_n

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
copy_n
(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 InputIt, class Size, class OutputIt >
OutputIt copy_n ( InputIt first, Size count, OutputIt result ) ;
(1) (C++11以降)
(C++20以降constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class Size, class ForwardIt2 >
ForwardIt2 copy_n ( ExecutionPolicy && policy,

ForwardIt1 first, Size count, ForwardIt2 result ) ;
(2) (C++17以降)
1) 範囲の先頭 first から count 個の値を正確にコピーし、範囲の先頭 result に書き込みます。形式的には、各整数 i について [ 0 , count ) の範囲で、 * ( result + i ) = * ( first + i ) を実行します。
範囲の重複は正式に許可されていますが、結果の順序が予測不可能になります。
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 - コピー元の要素範囲の先頭
count - コピーする要素の数
result - コピー先範囲の先頭
policy - 使用する 実行ポリシー
型要件
-
InputIt LegacyInputIterator の要件を満たさなければならない
-
OutputIt LegacyOutputIterator の要件を満たさなければならない
-
ForwardIt1, ForwardIt2 LegacyForwardIterator の要件を満たさなければならない

戻り値

宛先範囲のイテレータ。 count > 0 の場合、コピーされた最後の要素の次を指し、それ以外の場合は result を指します。

計算量

count < 0 の場合、ゼロ代入。 count それ以外の場合、count回の代入。

例外

ExecutionPolicy という名前のテンプレートパラメータを持つオーバーロードは、 以下のようにエラーを報告します:

  • アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、 ExecutionPolicy 標準ポリシー のいずれかであるとき、 std::terminate が呼び出されます。それ以外の ExecutionPolicy については、動作は実装定義です。
  • アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされます。

実装例

template<class InputIt, class Size, class OutputIt>
constexpr //< since C++20
OutputIt copy_n(InputIt first, Size count, OutputIt result)
{
    if (count > 0)
    {
        *result = *first;
        ++result;
        for (Size i = 1; i != count; ++i, (void)++result)
            *result = *++first;
    }
    return result;
}
HTMLタグ、属性、` `、`
`タグ内のテキスト、およびC++固有の用語は翻訳せず、元のフォーマットを保持しました。

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>
int main()
{
    std::string in {"1234567890"};
    std::string out;
    std::copy_n(in.begin(), 4, std::back_inserter(out));
    std::cout << out << '\n';
    std::vector<int> v_in(128);
    std::iota(v_in.begin(), v_in.end(), 1);
    std::vector<int> v_out(v_in.size());
    std::copy_n(v_in.cbegin(), 100, v_out.begin());
    std::cout << std::accumulate(v_out.begin(), v_out.end(), 0) << '\n';
}

出力:

1234
5050

関連項目

要素の範囲を新しい場所にコピーする
(関数テンプレート)
指定された数の要素を新しい場所にコピーする
(アルゴリズム関数オブジェクト)