Namespaces
Variants

std::ranges::generate

cppreference.net에서
 
 
알고리즘 라이브러리
제약된 알고리즘 및 범위에 대한 알고리즘 (C++20)
제약된 알고리즘, 예: ranges::copy, ranges::sort, ...
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 탐색 연산
(분할된 범위에 대해)
집합 연산 (정렬된 범위에 대해)
병합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
제약된 알고리즘
이 메뉴의 모든 이름은 네임스페이스에 속합니다 std::ranges
비수정 시퀀스 연산
수정 시퀀스 연산
분할 연산
정렬 연산
이진 탐색 연산 (정렬된 범위에 대해)
       
       
집합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
       
       
순열 연산
폴드 연산
수치 연산
(C++23)            
초기화되지 않은 저장소에 대한 연산
반환 타입
 
헤더에 정의됨 <algorithm>
호출 시그니처
template< std::input_or_output_iterator O, std::sentinel_for<O> S,
          std::copy_constructible F >
requires std::invocable<F&> && std::indirectly_writable<O, std::invoke_result_t<F&>>
constexpr O
    generate( O first, S last, F gen );
(1) (C++20 이후)
template< class R, std::copy_constructible F >
requires std::invocable<F&> && ranges::output_range<R, std::invoke_result_t<F&>>
constexpr ranges::borrowed_iterator_t<R>
    generate( R&& r, F gen );
(2) (C++20 이후)
1) 연속적인 함수 객체 gen의 호출 결과를 범위 [first, last)의 각 요소에 할당합니다.
2) (1)과 동일하지만 , 범위로 r을(를) 사용하며, ranges::begin(r)을(를) first로, ranges::end(r)을(를) last로 사용하는 것과 같습니다.

이 페이지에 설명된 함수형 엔티티는 알고리즘 함수 객체(비공식적으로 niebloids라고 함), 즉:

매개변수

first, last - 수정할 요소들의 범위 를 정의하는 반복자-감시자 쌍
r - 수정할 요소들의 범위
gen - 생성기 함수 객체

반환값

last 와 비교 시 동일함을 나타내는 출력 반복자입니다.

복잡도

정확히 ranges:: distance ( first, last ) 번의 gen ( ) 호출과 할당이 수행됩니다.

가능한 구현

struct generate_fn
{
    template<std::input_or_output_iterator O, std::sentinel_for<O> S,
             std::copy_constructible F>
    requires std::invocable<F&> && std::indirectly_writable<O, std::invoke_result_t<F&>>
    constexpr O operator()(O first, S last, F gen) const
    {
        for (; first != last; *first = std::invoke(gen), ++first)
        {}
        return first;
    }
    template<class R, std::copy_constructible F>
    requires std::invocable<F&> && ranges::output_range<R, std::invoke_result_t<F&>>
    constexpr ranges::borrowed_iterator_t<R> operator()(R&& r, F gen) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(gen));
    }
};
inline constexpr generate_fn generate {};

예제

#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 device;
    static std::mt19937 engine {device()};
    return distr(engine);
}
void iota(auto& r, int init)
{
    std::ranges::generate(r, [init] mutable { return init++; });
}
void print(std::string_view comment, const auto& v)
{
    for (std::cout << comment; int i : v)
        std::cout << i << ' ';
    std::cout << '\n';
}
int main()
{
    std::array<int, 8> v;
    std::ranges::generate(v.begin(), v.end(), dice);
    print("dice: ", v);
    std::ranges::generate(v, dice);
    print("dice: ", v);
    iota(v, 1);
    print("iota: ", v);
}

가능한 출력:

dice: 4 3 1 6 6 4 5 5
dice: 4 2 5 3 6 2 6 2
iota: 1 2 3 4 5 6 7 8

참고 항목

함수를 N번 적용한 결과를 저장함
(algorithm function object)
요소 범위에 특정 값을 할당함
(algorithm function object)
여러 요소에 값을 할당함
(algorithm function object)
요소 범위에 함수를 적용함
(algorithm function object)
균일 난수 비트 생성기에서 난수로 범위를 채움
(algorithm function object)
연속적인 함수 호출 결과를 범위의 모든 요소에 할당함
(function template)