I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStlAlgorithmsHeap

make_heap

Converts a range into a heap

Definition

The make_heap() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class RandomAccessIterator >
   void make_heap(
      RandomAccessIterator first, 
      RandomAccessIterator last
   );
template < class RandomAccessIterator, class BinaryPredicate >
   void make_heap(
      RandomAccessIterator first, 
      RandomAccessIterator last,
      BinaryPredicate comp
   );

Parameters:
Parameter Description
first A random-access iterator addressing the position of the first element in the range to be converted into a heap
last A random-access iterator addressing the position one past the final element in the range to be converted into a heap
comp User-defined predicate function object that defines sense in which one element is less than another. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied

Description

Make_heap() function reorders the elements between fi rst and last in order to create a heap.

The first version compares objects using operator< and the second compares objects using a function object comp.

Return Value

None.

Complexity

The complexity is linear; performs at most 3*(last - first) comparisons.
Example:

Example - make_heap algorithm
Problem
This program illustrates the use of the STL make_heap() algorithm (default version) to convert an arbitrary vector of integers into a heap.
Workings
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a[] = {25, 17, 36, 2, 3, 100, 1, 19, 7};
  vector<int> v(a, a+9);
 
  cout <<"\nHere are the values in the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  cout <<"\nNow we make these values into a (maximum) heap.";
  make_heap(v.begin(), v.end());
 
  cout <<"\nHere are the revised contents of the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  return 0;
}
Solution
Output:

Here are the values in the vector:
25 17 36 2 3 100 1 19 7

Now we make these values into a (maximum) heap.

Here are the revised contents of the vector:
100 19 36 17 3 25 1 2 7
References