Namespaces
Variants

std::ranges:: 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
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:: input_or_output_iterator O, std:: copy_constructible F >

requires std:: invocable < F & > && std:: indirectly_writable < O, std:: invoke_result_t < F & >>
constexpr O

generate_n ( O first, std:: iter_difference_t < O > n, F gen ) ;
(C++20以降)

関数オブジェクト gen 連続的な 呼び出しの結果を、範囲 [ first , first + n ) 内の各要素に代入します。ただし 0 < n の場合のみ有効で、それ以外の場合は何も行いません。

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

目次

パラメータ

first - 変更する要素範囲の先頭
n - 変更する要素の数
gen - ジェネレータ関数オブジェクト

戻り値

countが 0 < count の場合、割り当てられた最後の要素の次の位置を指すイテレータ、 first それ以外の場合。

計算量

正確に n 回の gen ( ) の呼び出しと代入が行われます。

実装例

struct generate_n_fn
{
    template<std::input_or_output_iterator O, std::copy_constructible F>
    requires std::invocable<F&> && std::indirectly_writable<O, std::invoke_result_t<F&>>
    constexpr O operator()(O first, std::iter_difference_t<O> n, F gen) const
    {
        for (; n-- > 0; *first = std::invoke(gen), ++first)
        {}
        return first;
    }
};
inline constexpr generate_n_fn generate_n {};

#include <algorithm>
#include <array>
#include <iostream>
#include <random>
#include <string_view>
auto dice()
{
    static std::uniform_int_distribution<int> distr {1, 6};
    static std::random_device engine;
    static std::mt19937 noise {engine()};
    return distr(noise);
}
void print(const auto& v, std::string_view comment)
{
    for (int i : v)
        std::cout << i << ' ';
    std::cout << '(' << comment << ")\n";
}
int main()
{
    std::array<int, 8> v;
    std::ranges::generate_n(v.begin(), v.size(), dice);
    print(v, "dice");
    std::ranges::generate_n(v.begin(), v.size(), [n {0}] mutable { return n++; });
    // same effect as std::iota(v.begin(), v.end(), 0);
    print(v, "iota");
}

出力例:

5 5 2 2 6 6 3 5 (dice)
0 1 2 3 4 5 6 7 (iota)

関連項目

関数の結果を範囲に保存する
(アルゴリズム関数オブジェクト)
一様乱数ビット生成器からの乱数で範囲を埋める
(アルゴリズム関数オブジェクト)
範囲の要素に特定の値を代入する
(アルゴリズム関数オブジェクト)
指定された数の要素に値を代入する
(アルゴリズム関数オブジェクト)
範囲の要素に関数を適用する
(アルゴリズム関数オブジェクト)
連続する関数呼び出しの結果を範囲内のN個の要素に代入する
(関数テンプレート)