std::generate ,顾名思义,它是一种STL算法,用于根据生成器函数生成数字,然后将这些值分配给[first,last]范围内容器中的元素。
null
生成器函数必须由用户定义,并连续调用以分配数字。
模板功能:
void generate (ForwardIterator first, ForwardIterator last, Generator gen); first: Forward iterator pointing to the first element of the container. last: Forward iterator pointing to the last element of the container. gen: A generator function, based upon which values will be assigned. Returns: none Since, it has a void return type, so it does not return any value.
// C++ program to demonstrate the use of std::generate #include <iostream> #include <vector> #include <algorithm> // Defining the generator function int gen() { static int i = 0; return ++i; } using namespace std; int main() { int i; // Declaring a vector of size 10 vector< int > v1(10); // using std::generate std::generate(v1.begin(), v1.end(), gen); vector< int >::iterator i1; for (i1 = v1.begin(); i1 != v1.end(); ++i1) { cout << *i1 << " " ; } return 0; } |
输出:
1 2 3 4 5 6 7 8 9 10
下一步: STD::C++中的GeaType
本文由 辛格先生 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END