Sort vector1 using make_heap and sort_heap : make_heap « STL Algorithms Heap « C++






Sort vector1 using make_heap and sort_heap

 
 

#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
 */        
  








Related examples in the same category

1.Use make_heap to convert collection into a heap
2.Use std::make_heap to create heap from a vector