std::ranges:: generate
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::
sentinel_for
<
O
>
S,
std::
copy_constructible
F
>
|
(1) | (C++20 이후) |
|
template
<
class
R,
std::
copy_constructible
F
>
requires
std::
invocable
<
F
&
>
&&
ranges::
output_range
<
R,
std::
invoke_result_t
<
F
&
>>
|
(2) | (C++20 이후) |
[
first
,
last
)
내의 각 요소에 할당합니다.
이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 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
참고 항목
|
(C++20)
|
함수를 N번 적용한 결과를 저장함
(algorithm function object) |
|
(C++20)
|
요소 범위에 특정 값을 할당함
(algorithm function object) |
|
(C++20)
|
여러 요소에 값을 할당함
(algorithm function object) |
|
(C++20)
|
요소 범위에 함수를 적용함
(algorithm function object) |
|
(C++26)
|
균일 난수 비트 생성기에서 난수로 범위를 채움
(algorithm function object) |
|
연속적인 함수 호출 결과를 범위의 모든 요소에 할당함
(function template) |