Source of push_heap1a.cpp


  1: //push_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 push_heap() "
 11:         "algorithm (default\nversion) to add a value to a (maximum) "
 12:         "heap of integers.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 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 add the value 100 to the heap.";
 24:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 25:     v.push_back(100);
 26:     push_heap(v.begin(), v.end());

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