Source of pop_heap2a.cpp


  1: //pop_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 pop_heap() "
 12:         "algorithm (extended\nversion) to delete the top (root) (highest "
 13:         "priority) (smallest) value from\na (minimum) heap of integers.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

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

 24:     cout << "\nNow we delete (pop) a value from the heap.";
 25:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 26:     pop_heap(v.begin(), v.end(), greater<int>());

 28:     cout << "\nHere are the revised contents of the vector:\n";
 29:     for (vector<int>::size_type i=0; i<v.size(); i++)
 30:         cout << v.at(i) << " ";
 31:     cout << "\nNote that the value deleted from the heap is still "
 32:         "\nin the vector (at the end of the vector).";
 33:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 35:     cout << "\nSo, we should reduce the size of the vector by 1, which "
 36:         "we now do, and\nthen display the vector, which is once again a "
 37:         "heap, one more time.";
 38:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 39:     v.resize(v.size()-1);

 41:     cout << "\nHere are the final contents of the vector:\n";
 42:     for (vector<int>::size_type i=0; i<v.size(); i++)
 43:         cout << v.at(i) << " ";
 44:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 45: }