Replaces each element with the result of an operation
You're viewing an older version of this page (#4142). View the current version.
View versions (1)
Definition
The generate() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
#include <algorithm>
template < class ForwardIterator, class Generator >
void generate(
ForwardIterator first,
ForwardIterator last,
Generator gen
);
Parameters:
| Parameter | Description |
| first | A forward iterator addressing the position of the first element in the range to which values are to be assigned |
| last | A forward iterator addressing the position one past the final element in the range to which values are to be assigned |
| gen | A function object that is called with no arguments that is used to generate the values to be assigned to each of the elements in the range |
Description
Generate algorithm uses a generator to generate each element of a range.
Return Value
None.
Complexity
The complexity is linear with last - first invocations of gen.
References
Example 1
ProblemThis program illustrates the use of the STL generate() algorithm to fill a range of values in a vector of integers with values generated by the rand() function from <cstdlib>.
Workings#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
vector<int> v(10);
cout <<"\nHere are the initial contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
cout <<"\nNow we fill v with randomly generated integer values.";
generate(v.begin(), v.end(), rand);
cout <<"\nHere are the revised contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
cout <<"\nFinally, we fill the last half of v with new randomly generated integer values.";
generate(v.begin()+5, v.end(), rand);
cout <<"\nHere are the revised contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
return 0;
}
SolutionOutput:
Here are the initial contents of v:
0 0 0 0 0 0 0 0 0 0
Now we fill v with randomly generated integer values.
Here are the revised contents of v:
41 18467 6334 26500 19169 15724 11478 29358 26962 24464
Finally, we fill the last half of v with new randomly generated integer values.
Here are the revised contents of v:
41 18467 6334 26500 19169 5705 28145 23281 16827 9961
Example 2
ProblemThe following program demonstrates how to use generate() and generate_n() to insert or assign some random numbers.
Workings#include <iostream>
#include <list>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
list<int> coll;
//insert five random numbers
generate_n (back_inserter(coll), //beginning of destination range
5, //count
rand); //new value generator
PRINT_ELEMENTS(coll);
//overwrite with five new random numbers
generate (coll.begin(), coll.end(), //destination range
rand); //new value generator
PRINT_ELEMENTS(coll);
return 0;
}
SolutionOutput:
41 18467 6334 26500 19169
15724 11478 29358 26962 24464
References- Nicolai M. Josuttis: "The C++ Standard Library"
See Also