Updated March 29, 2023
Introduction to C++ Generate()
The generator function(std::generate) in C++ helps generate numbers based on a generator function and assigns the values to the items in the container in the specified range [f, l). This function has to be described by the user and called successfully for the assignment of numbers. In this topic, we are going to learn about C++ Generate(). Let us look into this function in more detailed in the below sections.
Syntax
Below is the syntax of the generating function.
void generate ( ForwardIterator f, ForwardIterator l, Generator g ) ;
Parameters
f: Here, f is a forward iterator that points to the first item of the container.
l: Here, l is a forward iterator that points to the last item of the container.
g: This is the generator function based upon which values will be assigned.
Returns value: As the return type is void, no value will be returned.
Exceptions:
An exception will be thrown if generate function g or operation present on an iterator throws an exception.
Undefined behavior will be caused when invalid parameters are used.
Time complexity:
Linear between the first and last.
Data races:
The objects are modified in the range between first and last. Here, only one time, each object gets accessed.
How generate() algorithm function work in C++?
The following are the steps that have to be followed for the generator() function to work.
1. Create a vector of any size based on the user requirement.
vector<int> vt(12);
Here, vector v is of size 12.
2. Create a generate () method with three parameters: a pointer to the first item, a pointer to the second item, and a generator function.
generate(vt.begin(), vt.end(), rand);
Here, begin, end, and rand are the three parameters. For example, rand () is a function that generates random numbers.
3. Print all the elements using an iterator or loop based on the user requirement.
Examples of C++ Generate()
Let us see some of the sample programs on generate function in CPP.
Example #1
CPP program to generate random numbers within the limit using generate() and rand() functions.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
//create vector of size 10
vector<int> vt(10);
//generate random numbers
generate(vt.begin(), vt.end(), rand);
cout << "The random numbers in the vector: " << endl;
for (auto i = vt.begin(); i != vt.end(); ++i)
cout << *i << endl;
return 0;
}
Output:
In this program, a vector of size 10 is created first. Then random numbers are generated within the range of the vector size. As already mentioned, the generate() method has three parameters where the first parameter points to the first item, and the second parameter points to the last item. The third parameter is the generate function in which the rand() method is considered. rand() method helps in generating random numbers. Then all the elements get printed on executing the code.
Example #2
CPP program to implement generate function by passing a function as a parameter.
Code:
// C++ program to implement std::generate with generator function
#include <iostream>
#include <vector>
#include <algorithm>
// Define the generator function g
int g()
{
static int n = 0;
return ++n;
}
using namespace std;
int main()
{
int n;
// Declare a vector vtr of size 6
vector<int> vtr(6);
// usage of std::generate
std::generate(vtr.begin(), vtr.end(), g);
//iterator
vector<int>::iterator it;
for (it = vtr.begin(); it != vtr.end(); ++it) {
cout << *it << " ";
}
return 0;
}
Output:
In this program, a vector of size 6 is created first. Then generate() method is called where the third parameter is the generate function g that is defined separately. In the function g, numbers are incremented starting from 0 and end until 6. Here, 0 is not as inclusive as the variable gets incremented before printing it. After this, an iterator is used for printing all the elements. Finally, on executing the code, all the elements get printed, as shown in the result.
Example #3
CPP program to implement generate function by passing an instruction instead of a function.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
//create vector of size 10
vector<int> v(10);
int i(0);
//generate numbers
generate(v.begin() , v.end(), [&]{ return i++; }) ;
std::cout << "vector is: " ;
for (auto vv: v) {
std::cout << vv << " " ;
}
std::cout << " \n ";
}
Output:
Similar to the first program, in this program also, a vector of size 10 is created first. Then a variable i is initialized as 0. In the generate() method which has three parameters the first parameter is vector.begin(), and second parameter is vector.end(). The third parameter is the increment of i. Then all the elements get printed on executing the code.
Example #4
CPP program generates random numbers and unique numbers within the limit using generate() and rand() functions.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
//create vector of size 4
vector<int> vt(4);
//generate random numbers
generate(vt.begin(), vt.end(), rand);
cout << "The random numbers in the vector: " << endl;
for (auto i = vt.begin(); i != vt.end(); ++i)
cout << *i << endl;
int i(0);
//generate random numbers
generate(vt.begin(), vt.end(),[&]{ return i++; });
std::cout << "vector is: " ;
for (auto vv: vt) {
std::cout << vv << " " ;
}
std::cout << " \n ";
}
Output:
In this program, a vector of size 4 is created first. Then random numbers are generated within the range of the vector size using the rand() function inside the generate() function. Then, generate() function is again called where the third parameter is an instruction that increments a variable i. On executing the code, both the random numbers and unique numbers of range 4 gets printed.
Conclusion
The generator function (std:: generate) in C++ helps generate numbers based on a generator function and assigns the values to the items in the container in the specified range [f, l). In this article, more details on this function are explained in detail.
Recommended Articles
This is a guide to C++ Generate(). Here we discuss how the generate() algorithm function works in C++ with the syntax and parameters and Examples along with codes and outputs. You may also have a look at the following articles to learn more –