Adobe Systems Incorporated


Category: algorithms Component type: function


template <class ForwardIterator, class Generator>
void generate(ForwardIterator first, ForwardIterator last, Generator gen);


Generate assigns the result of invoking gen, a functors that takes no arguments, to each element in the range [first, last). [1]


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

Requirements on types

  • ForwardIterator is a model of ForwardIterator. [2]
  • ForwardIterator is mutable.
  • Generator is a model of Generator.
  • Generator's result type is convertible to ForwardIterator's value type.


  • [first, last) is a valid range.


Linear. Exactly last - first invocations of gen. [1]


Fill a vector with random numbers, using the standard C library function rand.

vector<int> V;
generate(V.begin(), V.end(), rand);


[1] The functors gen is invoked for each iterator in the range [first, last), 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.

[2] The reason that generate requires its argument to be a mutable ForwardIterator, rather than just an OutputIterator, is that it uses a range [first, last) of iterators. There is no sensible way to describe a range of OutputIterator, because it is impossible to compare two OutputIterator for equality. The generate_n algorithm does have an interface that permits use of an OutputIterator.

See also

copy, fill, fill_n, generate_n, 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