generate

Syntax:

    #include <algorithm>
    void generate( iterator start, 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 (size_t i = 0; i < v.size(); ++i)
        cout << v[i] << ' ';
    cout << endl;

Related Topics: copy, fill, generate_n, transform