stl:algorithm:fill

C++ Reference

fill

Syntax:

    #include <algorithm>
    void fill( iterator start, iterator end, const TYPE& val );

The function fill() assigns val to all of the elements between start and end.

For example, the following code uses fill() to set all of the elements of a vector of integers to -1:

   vector<int> v1;
   for( int i = 0; i < 10; i++ ) {
     v1.push_back( i );
   }
 
   cout << "Before, v1 is: ";
   for( unsigned int i = 0; i < v1.size(); i++ ) {
     cout << v1[i] << " ";
   }
   cout << endl;
 
   fill( v1.begin(), v1.end(), -1 );
 
   cout << "After, v1 is: ";
   for( unsigned int i = 0; i < v1.size(); i++ ) {
     cout << v1[i] << " ";
   }
   cout << endl;

When run, the above code displays:

   Before, v1 is: 0 1 2 3 4 5 6 7 8 9
   After, v1 is: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Related Topics: fill_n, generate, transform