std::generate
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |  | 该页由英文版wiki使用Google Translate机器翻译而来。 
 该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. | 
| 定义于头文件  <algorithm> | ||
| template< class ForwardIt, class Generator > void generate( ForwardIt first, ForwardIt last, Generator g ); | ||
分配范围
[first, last)生成的值给定的函数对象中的每个元素g | 目录 | 
[编辑] 参数
| first, last | - | |||||||
| g | - | 将要调用的生成器函数。 签名应等价于下列者: 
 类型 Ret 必须使得能解引用 ForwardIt 类型对象并对它赋 Ret 类型值。  | ||||||
| 类型要求 | ||||||||
| - ForwardIt必须满足 ForwardIterator的要求。 | ||||||||
[编辑] 返回值
[编辑] 复杂度
究竟std::distance(first, last)
g()和任务的调用.原文:
Exactly std::distance(first, last) invocations of 
g() and assignments.[编辑] 可能的实现
| template<class ForwardIt, class Generator> void generate(ForwardIt first, ForwardIt last, Generator g) { while (first != last) { *first++ = g(); } } | 
[编辑] 示例
 下面的代码使用填补了矢量随机数
 
运行此代码
#include <algorithm> #include <iostream> #include <cstdlib> int main() { std::vector<int> v(5); std::generate(v.begin(), v.end(), std::rand); // Using the C function rand() std::cout << "v: "; for (auto iv: v) { std::cout << iv << " "; } std::cout << "\n"; }
输出:
v: 52894 15984720 41513563 41346135 51451456
[编辑] 另请参阅
| 将一个值赋给一个范围内的元素 (函数模板) | |
| 保存函数的N次运行结果 (函数模板) | 


