Source of is_heap_until2a.cpp


  1: //is_heap_until2a.cpp

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

 10: int main()
 11: {
 12:     cout << "\nThis program illustrates the use of the STL "
 13:         "is_heap_until() algorithm from\n<algorithm> and the "
 14:         "greater<int>() functor from <functional> to find "
 15:         "the first\ninteger that destroys the minimum heap "
 16:         "order of a vector of integers.";
 17:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 19:     int a[] = {3, 8, 5, 9, 12, 14, 6, 17, 15};
 20:     int b[] = {3, 8, 5, 9, 6, 14, 12, 17, 15};
 21:     vector<int> va(a, a+9);
 22:     vector<int> vb(b, b+9);
 23:     cout << boolalpha;
 24:     auto firstNonHeapElementIter =
 25:         is_heap_until(va.begin(), va.end(), greater<int>());
 26:     if (firstNonHeapElementIter == va.end()) 
 27:         cout << "For 3, 8, 5, 9, 12, 14, 6, 17, 15, the sequence is "
 28:             "a minimum heap." << endl;
 29:     else
 30:         cout << "For 3, 8, 5, 9, 12, 14, 6, 17, 15, the minimum heap order "
 31:         "was destroyed\nat element " << *firstNonHeapElementIter << ".\n";

 33:     firstNonHeapElementIter =
 34:         is_heap_until(vb.begin(), vb.end(), greater<int>());
 35:     if (firstNonHeapElementIter == vb.end()) 
 36:         cout << "For 3, 8, 5, 9, 6, 14, 12, 17, 15, the sequence is "
 37:             "a minimum heap." << endl;
 38:     else
 39:         cout << "For 3, 8, 5, 9, 6, 14, 12, 17, 15, the minimum heap order "
 40:         "was destroyed\nat element " << *firstNonHeapElementIter << ".\n";
 41:     cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
 42: }