Source of partial_sort1a.cpp


  1: //partial_sort1a.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:         "partial_sort() algorithm (default\nversion) to partially "
 12:         "sort a vector of integers of size 12 by getting its\n5 smallest "
 13:         "values into ascending order at the beginning of the vector. "
 14:         "The\nfollowing range of values may also be sorted (by chance), "
 15:         "but the algorithm\ndoes not guarantee this, and you should not "
 16:         "expect it";
 17:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 26:     cout << "\nNow we make the following call:";
 27:     cout << "\npartial_sort(v.begin(), v.begin()+5, v.end());";
 28:     partial_sort(v.begin(), v.begin()+5, v.end());
 29:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 31:     cout << "\nAnd here are the (partially sorted) contents of the "
 32:         "vector,\nup to and including its 5th element:\n";
 33:     for (vector<int>::size_type i=0; i<v.size(); i++)
 34:         cout << v.at(i) << " ";
 35:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 36: }