Name

generate_n function template — Fills a counted range with values returned from a function

Synopsis

template<typename OutIter, typename Size, typename Generator>
  void generate_n(OutIter first, Size n, Generator gen);

The generate_n function template calls gen( ) exactly n times, assigning the results to fill the sequence that starts at first. You must ensure that the sequence has room for at least n items. The Size type must be convertible to an integral type.

Example

Example 13-3 shows a simple way to print a sequence of integers.

Example 13-3. Using generate_n to print a series of integers

#include <algorithm>
#include <iostream>
#include <iterator>
  
// Use the same series template fromExample 13-2.
  
int main(  )
{
  // Print integers from 1 to 10.
  std::generate_n(std::ostream_iterator<int>(std::cout,"\n"),
                  10, series<int>(1));
}