Replaces n elements with the result of an operation
You're viewing an older version of this page (#4143). View the current version.
View versions (1)
Definition
The generate_n() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
#include <algorithm>
template < class OutputIterator, class Diff, class Generator >
void generate_n(
OutputIterator first,
Diff count,
Generator gen
);
Parameters:
| Parameter | Description |
| first | An output iterator addressing the position of first element in the range to which values are to be assigned |
| count | A signed or unsigned integer type specifying the number of elements to be assigned a value by the generator function |
| 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_n algorithm does the same thing that http://www.codecogs.com/reference/computing/stl/algorithms/modifying/generate.php?showv=1"generate" but with n elements after the first.
Return Value
None.
Complexity
The complexity is linear; count calls to the generator being required.
References
Example 1
ProblemThis program illustrates the functionality of generate_n() algorithm.
Workings#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
#include <ostream>
using namespace std;
int main()
{
// Assigning random values to vector integer elements
vector <int> v1 ( 5 );
vector <int>::iterator Iter1;
deque <int> deq1 ( 5 );
deque <int>::iterator d1_Iter;
generate_n ( v1.begin ( ), 5 , rand );
cout <<"Vector v1 is ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout <<*Iter1<<" ";
cout <<")."<<endl;
// Assigning random values to deque integer elements
generate_n ( deq1.begin ( ), 3 , rand );
cout <<"Deque deq1 is ( " ;
for ( d1_Iter = deq1.begin( ) ; d1_Iter != deq1.end( ) ; d1_Iter++ )
cout <<*d1_Iter<<" ";
cout <<")."<<endl;
return 0;
}
SolutionOutput:
Vector v1 is ( 41 18467 6334 26500 19169 ).
Deque deq1 is ( 15724 11478 29358 0 0 ).
Example 2
ProblemThis program illustrates the use of the STL generate_n() algorithm to fill a range of values starting at a given location in a vector of integers with a specified number of 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 generate 6 random integer values and place them into the vector,"
" starting at location 3.";
generate_n(v.begin()+2, 6, 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 v with newly generated random integer values.";
generate_n(v.begin(), (int)v.size(), 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 generate 6 random integer values and place them into the vector, starting at location 3.
Here are the revised contents of v:
0 0 41 18467 6334 26500 19169 15724 0 0
Finally, we fill v with newly generated random integer values.
Here are the revised contents of v:
11478 29358 26962 24464 5705 28145 23281 16827 9961 491
See Also