Adobe Systems Incorporated


Category: algorithms Component type: function


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


Generate_n assigns the result of invoking gen, a functors that takes no arguments, to each element in the range [first, first+n). [1] The return value is first + n.


Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

  • OutputIterator is a model of OutputIterator.
  • Size is an integral type (either signed or unsigned).
  • Generator is a model of Generator.
  • Generator's result type is convertible to a type in OutputIterator's set of value types.


  • n >= 0.
  • There is enough space to hold n values. That is, [first, first+n) is a valid range.


Linear. Exactly n invocations of gen. [1]


Print 100 random numbers, using the C standard library function rand.

generate_n(ostream_iterator<int>(cout, "\n"), 100, rand);


[1] The functors gen is invoked n times (once for each iterator in the range [first, first+n)), as opposed to just being invoked a single time outside the loop. This distinction is important because a Generator need not return the same result each time it is invoked; it is permitted to read from a file, refer to and modify local state, and so on.

See also

copy, fill, fill_n, generate, iota

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google