Use std::make_heap to create heap from a vector : make_heap « STL Algorithms Heap « C++






Use std::make_heap to create heap from a vector

 
 

#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
   int a[ 10 ] = { 3, 10, 52, 77, 22, 31, 1, 98, 13, 40 };
   std::vector< int > v( a, a + 10 ); // copy of a
   std::vector< int > v2;
   std::ostream_iterator< int > output( cout, " " );

   std::copy( v.begin(), v.end(), output );

   std::make_heap( v.begin(), v.end() ); // create heap from vector v
   cout << "\n\n";

   std::copy( v.begin(), v.end(), output );

   cout << endl;
   return 0;
}

 /* 
3 10 52 77 22 31 1 98 13 40

98 77 52 13 40 31 1 10 3 22

 */       
  








Related examples in the same category

1.Use make_heap to convert collection into a heap
2.Sort vector1 using make_heap and sort_heap