Source of fill1a.cpp


  1: //fill1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <algorithm>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL fill() "
 11:         "algorithm to set all\nvalues in a range of values within "
 12:         "a vector of integers to a given value.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 16:     vector<int> v(a, a+10);
 17:     cout << "\nHere are the contents of v:\n";
 18:     for (vector<int>::size_type i=0; i<v.size(); i++)
 19:         cout << v.at(i) << " ";
 20:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 22:     cout << "\nNow we set the 3rd to the 7th values of v to 20.";
 23:     fill(v.begin()+2, v.begin()+7, 20);
 24:     cout << "\nHere are the revised contents of v:\n";
 25:     for (vector<int>::size_type i=0; i<v.size(); i++)
 26:         cout << v.at(i) << " ";
 27:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 29:     cout << "\nNow we set all values from the 6th on to -1.";
 30:     fill(v.begin()+5, v.end(), -1);
 31:     cout << "\nHere are the revised contents of v:\n";
 32:     for (vector<int>::size_type i=0; i<v.size(); i++)
 33:         cout << v.at(i) << " ";
 34:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 36:     cout << "\nFinally, we initialize the entire vector to 0.";
 37:     fill(v.begin(), v.end(), 0);
 38:     cout << "\nHere are the revised contents of v:\n";
 39:     for (vector<int>::size_type i=0; i<v.size(); i++)
 40:         cout << v.at(i) << " ";
 41:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 42: }