Source of pop_heap1a.cpp


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

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

 18:     cout << "\nHere are the values in the vector (the heap):\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 delete (pop) a value from the heap.";
 24:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 25:     pop_heap(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 << "\nNote that the value deleted from the heap is still "
 31:         "\nin the vector (at the end of the vector).";
 32:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

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

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