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 인 경우에만 해당됩니다. 그렇지 않으면 아무 작업도 수행하지 않습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first - 수정할 요소 범위의 시작
n - 수정할 요소의 개수
gen - 생성기 함수 객체

반환값

할당된 마지막 요소의 다음 위치를 가리키는 반복자. 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개 요소에 할당
(함수 템플릿)