Namespaces
Variants

std:: generate_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
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
generate_n
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 OutputIt, class Size, class Generator >
OutputIt generate_n ( OutputIt first, Size count, Generator g ) ;
(1) (constexpr since C++20)
template < class ExecutionPolicy,

class ForwardIt, class Size, class Generator >
ForwardIt generate_n ( ExecutionPolicy && policy,

ForwardIt first, Size count, Generator g ) ;
(2) (since C++17)
1) 指定された関数オブジェクト g によって生成された値を、 first から始まる範囲の最初の count 個の要素に代入する。 count > 0 の場合のみ実行され、それ以外の場合は何も行わない。
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以降)

Size integral type convertible でない場合、プログラムは不適格となります。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - その他のテキスト(Parameters、Return value、Complexityなど)はC++関連の専門用語として翻訳せず、原文のまま保持しました - HTMLタグ、属性、クラス名、ID、リンク先などは完全に保持されています - 数値や書式設定も変更していません

パラメータ

first - 生成する要素範囲の先頭
count - 生成する要素の数
policy - 使用する 実行ポリシー
g - 呼び出されるジェネレータ関数オブジェクト

関数のシグネチャは以下と同等であること:

Ret fun ( ) ;

Ret は、 OutputIt 型のオブジェクトが逆参照可能であり、型 Ret の値を代入できるものでなければならない

型要件
-
OutputIt LegacyOutputIterator の要件を満たさなければならない
-
ForwardIt LegacyForwardIterator の要件を満たさなければならない

戻り値

count > 0 の場合、代入された最後の要素の次を指すイテレータ、 first の場合、それ以外。

計算量

正確に std:: max ( 0 , count ) 回の g ( ) の呼び出しと代入が行われます。

例外

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

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

実装例

template<class OutputIt, class Size, class Generator>
constexpr // C++20以降
OutputIt generate_n(OutputIt first, Size count, Generator g)
{
    for (Size i = 0; i < count; ++i, ++first)
        *first = g();
    return first;
}

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
int main()
{
    std::mt19937 rng; // default constructed, seeded with fixed seed
    std::generate_n(std::ostream_iterator<std::mt19937::result_type>(std::cout, " "),
                    5, std::ref(rng));
    std::cout << '\n';
}

出力:

3499211612 581869302 3890346734 3586334585 545404204

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 426 C++98 計算量要件が「正確に count 回の呼び出し
または代入」であり、 count が負の場合に破綻する
count が非正の場合、
呼び出しまたは代入は行われない
LWG 865 C++98 生成範囲に続く最初の要素の位置が
返されなかった
返される

関連項目

指定された値を範囲内のN個の要素にコピー代入する
(関数テンプレート)
連続する関数呼び出しの結果を範囲内のすべての要素に代入する
(関数テンプレート)
関数のN回の適用結果を保存する
(アルゴリズム関数オブジェクト)