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
)
中的每个元素。
本页面描述的函数式实体是 算法函数对象 (非正式称为 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 次调用的结果
(算法函数对象) |
|
(C++20)
|
为范围内的元素赋予特定值
(算法函数对象) |
|
(C++20)
|
为指定数量的元素赋值
(算法函数对象) |
|
(C++20)
|
对范围内的元素应用函数
(算法函数对象) |
|
(C++26)
|
使用均匀随机位生成器产生的随机数填充范围
(算法函数对象) |
|
将连续函数调用的结果赋值给范围内的每个元素
(函数模板) |