Translations of this page?:

generate_n

Syntax:

    #include <algorithm>
    void generate_n( output_iterator result, SIZE num, Generator g );

The generate_n() function runs the Generator function object g num times, saving the result of each execution in result, (result+1), etc.

For example, the following code uses generate_n() to fill an array of int with random numbers using the standard C library rand function:

 #include <cstddef>
 #include <cstdlib>
 #include <iostream>
 #include <iterator>
 #include <algorithm>
 
 int main()
 {
   const std::size_t N = 5;
   int ar[N];
   std::generate_n(ar, N, std::rand); // Using the C function rand()
 
   std::cout << "ar: ";
   std::copy(ar, ar+N, std::ostream_iterator<int>(std::cout, " "));
   cout << endl;
 }

Related Topics: generate

 
• • • IndexRecent changesRSScc