Use generate_n to list to Random Values : generate_n « STL Algorithms Modifying sequence operations « C++ Tutorial






#include <algorithm>
#include <vector>
#include <list>
#include <iostream>

using namespace std;

int main (){

    list <int> l (10);
    generate_n (l.begin (), 5, rand);

    cout << "Elements in the list of size: " << l.size ();
    list <int>::const_iterator li;
    
    for ( li = l.begin (); li != l.end (); ++ li )
        cout << *li << ' ';


    return 0;
}








24.4.generate_n
24.4.1.Provide std::generate_n with predicate
24.4.2.Use std::generate_n to fill a vector by number
24.4.3.generate_n and int sequence
24.4.4.Use generate_n to insert five random numbers into a list
24.4.5.Use generate_n to list to Random Values
24.4.6.Demonstrating fill, fill_n, generate, and generate_n Standard Library methods.