Source of sort_heap2a.cpp


  1: //sort_heap2a.cpp

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

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the STL sort_heap() "
 12:         "algorithm (extended\nversion) to sort a (minimum) heap of "
 13:         "integers into descending order. In this\ncase, the built-in "
 14:         "predicate functor greater<int>() is used to order the\n"
 15:         "values in the vector which holds the heap.";
 16:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 18:     int a[] = {1, 2, 25, 7, 3, 100, 36, 19, 17};
 19:     vector<int> v(a, a+9);

 21:     cout << "\nHere are the values in the heap:\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 sort these values into descending order.";
 27:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 28:     sort_heap(v.begin(), v.end(), greater<int>());

 30:     cout << "\nHere are the results:\n";
 31:     for (vector<int>::size_type i=0; i<v.size(); i++)
 32:         cout << v.at(i) << " ";
 33:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 34: }