Source of generate1a.cpp


  1: //generate1a.cpp

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

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the STL generate() "
 12:         "algorithm to fill\na range of values in a vector of integers "
 13:         "with values generated by the\nrand() function from <cstdlib>.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     vector<int> v(10);

 18:     cout << "\nHere are the initial contents of v:\n";
 19:     for (vector<int>::size_type i=0; i<v.size(); i++)
 20:         cout << v.at(i) << " ";
 21:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 23:     cout << "\nNow we fill v with randomly generated integer values.";
 24:     generate(v.begin(), v.end(), rand);
 25:     cout << "\nHere are the revised contents of v:\n";
 26:     for (vector<int>::size_type i=0; i<v.size(); i++)
 27:         cout << v.at(i) << " ";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     cout << "\nFinally, we fill the last half of v with new randomly "
 31:         "generated integer values.";
 32:     generate(v.begin()+5, v.end(), rand);
 33:     cout << "\nHere are the revised contents of v:\n";
 34:     for (vector<int>::size_type i=0; i<v.size(); i++)
 35:         cout << v.at(i) << " ";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 37: }