std::ranges:: generate_n
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
&
>>
|
(C++20 起) | |
若
0
<
n
,则将函数对象
gen
的连续调用结果赋值给范围
[
first
,
first
+
n
)
中的每个元素。否则不执行任何操作。
本页面描述的函数式实体是 算法函数对象 (非正式称为 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)
参见
|
(C++20)
|
将函数调用结果保存到范围中
(算法函数对象) |
|
(C++26)
|
使用均匀随机位生成器产生的随机数填充范围
(算法函数对象) |
|
(C++20)
|
为范围中的元素赋予指定值
(算法函数对象) |
|
(C++20)
|
为指定数量的元素赋予某个值
(算法函数对象) |
|
(C++20)
|
对范围内的元素应用函数
(算法函数对象) |
|
将连续函数调用的结果赋值给范围中的 N 个元素
(函数模板) |