Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Algorithms
algorithm:
adjacent_find
binary_search
copy
copy_backward
count
count_if
equal
equal_range
fill
fill_n
find
find_end
find_first_of
find_if
for_each
generate
generate_n
includes
inplace_merge
iter_swap
lexicographical_compare
lower_bound
make_heap
max
max_element
merge
min
min_element
mismatch
next_permutation
nth_element
partial_sort
partial_sort_copy
partition
pop_heap
prev_permutation
push_heap
random_shuffle
remove
remove_copy
remove_copy_if
remove_if
replace
replace_copy
replace_copy_if
replace_if
reverse
reverse_copy
rotate
rotate_copy
search
search_n
set_difference
set_intersection
set_symmetric_difference
set_union
sort
sort_heap
stable_partition
stable_sort
swap
swap_ranges
transform
unique
unique_copy
upper_bound


generate_n

function template
<algorithm>
template <class OutputIterator, class Size, class Generator>
  void generate_n ( OutputIterator first, Size n, Generator gen );

Generate values for sequence with function

Sets the value of the first n elements in the sequence pointed by first to the value returned by successive calls to gen.

The behavior of this function template is equivalent to:
1
2
3
4
5
template <class OutputIterator, class Size, class Generator>
  void generate_n ( OutputIterator first, Size n, Generator gen )
{
  for (; n>0; --n)  *first++ = gen();
}


Parameters

first
Output iterators to the initial positions in a sequence of at least n elements.
n
Number of elements to set value to.
Its type is an integral type or some other type convertible to it.
gen
Generator function that takes no arguments and returns some value to be stored as element value. This can either be a pointer to a function or an object whose class overloads operator().

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// generate_n example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int current(0);
int UniqueNumber () { return ++current; }
int main () {
  int myarray[9];
  generate_n (myarray, 9, UniqueNumber);
  cout << "myarray contains:";
  for (int i=0; i<9; ++i)
    cout << " " << myarray[i];
  cout << endl;
  return 0;
}


A possible output:
myarray contains: 1 2 3 4 5 6 7 8 9

Complexity

Linear: Invokes gen and assigns that value, n times.

See also