std::generate
来自cppreference.com
![]() | 该页由英文版wiki使用Google Translate机器翻译而来。 该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
定义于头文件 <algorithm> | ||
template<class ForwardIt, class Generator > void generate( ForwardIt first, ForwardIt last, Generator g ); | ||
分配范围
[first, last)
生成的值给定的函数对象中的每个元素g
原文:
Assigns each element in range
[first, last)
a value generated by the given function object g
. 目录 |
[编辑]参数
first, last | - | 的范围内的元素来生成 | ||||||
g | - | generator function object that will be called. The signature of the function should be equivalent to the following:
The type Ret must be such that an object of type ForwardIt can be dereferenced and assigned a value of type 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();}} |
[编辑]示例
下面的代码使用填补了矢量随机数
原文:
The following code uses fills a vector with random numbers:
#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次运行结果 (函数模板) |