Source of random_shuffle1a.cpp


  1: //random_shuffle1a.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 "
 11:         "random_shuffle() algorithm\n(default version) to "
 12:         "randomize the order of values in a vector of integers.";
 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);

 18:     cout << "\nHere are the values in the vector:\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 randomize the order of the values.";
 24:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 25:     random_shuffle(v.begin(), v.end());

 27:     cout << "\nHere are the revised contents of the vector:\n";
 28:     for (vector<int>::size_type i=0; i<v.size(); i++)
 29:         cout << v.at(i) << " ";
 30:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 31: }