std::generate_n in C++ Last Updated : 21 Jul, 2017 Comments Improve Suggest changes Like Article Like Report std::generate 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. Now, there can be a scenario, where we want to assign values only to the first n elements, for that we have another STL algorithm std::generate_n, which has the following syntax: Template function: OutputIterator generate_n (OutputIterator first, Size n, Generator gen); first: Output iterator pointing to the beginning of the container. n: No. of elements to be assigned a value, using generator function. gen: A generator function for generating the values. Returns: It doesnot have a void return type like std::generate, but, in fact, it returns an iterator pointing to the element that follows the last element whose value has been generated. CPP // C++ program to demonstrate the use of std::generate_n #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_n std::generate_n(v1.begin(), 10, 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 Comment More infoAdvertise with us Next Article std::generate_n in C++ K kartik Improve Article Tags : C++ STL cpp-algorithm-library Practice Tags : CPPSTL Similar Reads std::generate in C++ 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 successive 2 min read random header in C++ | Set 1(Generators) This header introduces random number generation facilities. This library allows to produce random numbers using combinations of generators and distributions. Generators: Objects that generate uniformly distributed numbers.Distributions: Objects that transform sequences of numbers generated by a gene 11 min read copy_n() Function in C++ STL Copy_n() is the C++ function defined in <algorithm> library in STL. It helps to copy one array element to the new array. Copy_n function allows the freedom to choose how many elements must be copied in the destination container. This function takes 3 arguments, the source array name, the size 2 min read STD::array in C++ The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar 5 min read make_pair() in C++ STL In C++, make_pair() is a standard library function used to construct a key-value pair from the given arguments. The type of the pair constructed is deduced automatically from the type of arguments. In this article, we will learn about make_pair() function in C++.Letâs take a quick look at a simple e 3 min read Like