Sort vector1 using make_heap and sort_heap : make_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;

  // Shuffle the elements again:
  random_shuffle(vector1.begin(), vector1.end());


  make_heap(vector1.begin(), vector1.end());
  sort_heap(vector1.begin(), vector1.end());

  // Verify that the array is sorted:
  for (int i = 0; i < 5; ++i)
     cout << vector1[i];

  return 0;
}
01234








31.1.make_heap
31.1.1.Use make_heap to convert collection into a heap
31.1.2.Use std::make_heap to create heap from a vector
31.1.3.Sort vector1 using make_heap and sort_heap