Source of fill_n1a.cpp


  1: //fill_n1a.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_n() "
 11:         "algorithm\nto set a given number values starting at a given "
 12:         "location in a\nvector 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 5 values of v, starting at the 3rd, to 50.";
 23:     fill_n(v.begin()+2, 5, 50);
 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 the last three values of v to -1.";
 30:     fill_n(v.begin()+7, 3, -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 100.";
 37:     fill_n(v.begin(), v.size(), 100);
 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: }