Sort vector1 using push_heap and pop_heap : push_heap « STL Algorithms Heap « C++ Tutorial






#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
  vector<int> vector1(5);
  for (int i = 0; i < 5; ++i)
    vector1[i] = i;

  random_shuffle(vector1.begin(), vector1.end());

  
  for (int i = 2; i <= 5; ++i)
    push_heap(vector1.begin(), vector1.begin() + i);
  
  for (int i = 5; i >= 2; --i)
    pop_heap(vector1.begin(), vector1.begin() + i);
  
  for (int i = 0; i < 5; ++i)   
    cout << vector1[i];

  return 0;
}
01234








31.3.push_heap
31.3.1.Use push_heap to push new element into the heap
31.3.2.Sort vector1 using push_heap and pop_heap