generate [C++ Reference]
Keywords: stl,algorithm,generate
Date: 2009-10-09T01:31:18-0700

[[generate]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## 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

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •