Source of shuffle1a.cpp


  1: //shuffle1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <algorithm>
  6: #include <iterator>
  7: #include <random>
  8: #include <ctime>
  9: using namespace std;

 11: int main()
 12: {
 13:     cout << "\nThis program illustrates the use of the STL "
 14:         "shuffle() algorithm from\n<algorithm> to randomly "
 15:         "shuffle the first ten positive integers.";
 16:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 18:     default_random_engine dre;

 20:     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 21:     cout << endl;
 22:     copy(begin(a), end(a), ostream_iterator<int>(cout, " "));
 23:     cout << endl << endl;

 25:     vector<int> v1(begin(a), end(a));
 26:     shuffle(v1.begin(), v1.end(), dre);
 27:     copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
 28:     cout << endl;

 30:     dre.seed(time(0));
 31:     vector<int> v2(begin(a), end(a));
 32:     shuffle(v2.begin(), v2.end(), dre);
 33:     copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
 34:     cout << endl;

 36:     dre.seed();
 37:     vector<int> v3(begin(a), end(a));
 38:     shuffle(v3.begin(), v3.end(), dre);
 39:     copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, " "));
 40:     cout << endl;
 41:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 42: }