I have forgotten

• https://me.yahoo.com

# //

Replaces n elements with the result of an operation
View version details

## 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 generate but with `n` elements after the `first`.

None.

## Complexity

The complexity is linear; `count` calls to the generator being required.

### References

Example:

##### Example - generate_n algorithm
Problem
This 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;
}```
Solution
Output:

Vector v1 is ( 41 18467 6334 26500 19169 ).
Deque deq1 is ( 15724 11478 29358 0 0 ).
References