std::generate in C++ Last Updated : 21 Jul, 2017 Comments Improve Suggest changes 5 Likes Like Report std::generate, as the name suggests is an STL algorithm, which is used to generate numbers based upon a generator function, and then, it assigns those values to the elements in the container in the range [first, last). The generator function has to be defined by the user, and it is called successively for assigning the numbers. Template function: 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. CPP // 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; } Output: 1 2 3 4 5 6 7 8 9 10 Next: std::generate_n in C++ Create Quiz Comment M Mrigendra Singh 5 Improve M Mrigendra Singh 5 Improve Article Tags : Misc C++ STL cpp-algorithm-library Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like