Namespaces
Variants

std:: generate

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
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 ForwardIt, class Generator >
void generate ( ForwardIt first, ForwardIt last, Generator g ) ;
(1) (constexpr since C++20)
template < class ExecutionPolicy, class ForwardIt, class Generator >

void generate ( ExecutionPolicy && policy,

ForwardIt first, ForwardIt last, Generator g ) ;
(2) (since C++17)
1) 범위 [ first , last ) 내의 각 요소에 주어진 함수 객체 g 가 생성한 값을 할당합니다.
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, last - 요소를 생성할 범위 를 정의하는 반복자 쌍
policy - 사용할 실행 정책
g - 호출될 생성자 함수 객체.

함수의 시그니처는 다음에 해당해야 합니다:

Ret fun ( ) ;

Ret 타입은 ForwardIt 타입의 객체가 역참조되고 Ret 타입의 값을 할당할 수 있어야 합니다. ​

타입 요구사항
-
ForwardIt LegacyForwardIterator 의 요구사항을 충족해야 합니다.

복잡도

정확히 std:: distance ( first, last ) 번의 g ( ) 호출 및 할당이 수행됩니다.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.

가능한 구현

template<class ForwardIt, class Generator>
constexpr //< since C++20
void generate(ForwardIt first, ForwardIt last, Generator g)
{
    for (; first != last; ++first)
        *first = g();
}

예제

#include <algorithm>
#include <iostream>
#include <vector>
void println(std::string_view fmt, const auto& v)
{
    for (std::cout << fmt; const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
};
int f()
{
    static int i;
    return ++i;
}
int main()
{
    std::vector<int> v(5);
    std::generate(v.begin(), v.end(), f);
    println("v: ", v);
    // 람다 함수로 기본값 0,1,2,3,4로 초기화
    // std::iota(v.begin(), v.end(), 0); 와 동일
    std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });
    println("v: ", v);
}

출력:

v: 1 2 3 4 5
v: 0 1 2 3 4

참고 항목

주어진 값을 범위의 모든 요소에 복사-할당합니다
(function template)
연속적인 함수 호출 결과를 범위의 N개 요소에 할당합니다
(function template)
(C++11)
시작 값의 연속적인 증가값으로 범위를 채웁니다
(function template)
함수의 결과를 범위에 저장합니다
(algorithm function object)