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:
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.
1 2 3 4 5 6 7 8 9 10
This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find the K closest points to origin using Priority Queue
- basic_istream::readsome() in C++ with Examples
- std::is_function template in C++ with Examples
- type_traits::is_null_pointer in C++
- std::extent() template in C++ with Examples
- basic_istream::swap() in C++ with Examples
- basic_istream::peek() in C++ with Examples
- basic_istream::unget() in C++ with Examples
- exception::bad_exception in C++ with Examples
- std::bad_array_new_length class in C++ with Examples
- basic_istream::seekg() in C++ with Examples
- functional::bad_function_call in C++ with Examples
- basic_istream::get() in C++ with Examples
- typeinfo::bad_cast in C++ with Examples
- basic_istream::putback() in C++ with Examples