generate

Syntax:

    #include <algorithm>
    void generate( forward_iterator start, forward_iterator end, Generator g );

The generate() function runs the Generator function object g a number of times, saving the result of each execution in the range [start,end).

For example, the following code uses generate() to fill a vector with random numbers using the standard C library rand function:

    vector<int> v(5);
    generate(v.begin(), v.end(), rand); // Using the C function rand()
    // Depending on the compiler you may need to put &rand instead
 
    cout << "v: ";
    for (vector<int>::size_type i = 0; i < v.size(); ++i)
        cout << v[i] << ' ';
    cout << endl;

Related Topics: copy, fill, generate_n, transform