make_heap


Make a sequence into a heap.

Arrange the elements in the range [ first ... last ) into a heap. The first version uses operator< to perform the comparisons, whereas the second version uses the binary function compare .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class RandomAccessIterator >
void make_heap
  (
  RandomAccessIterator first,
  RandomAccessIterator last
  );

template< class RandomAccessIterator, class Compare >
void make_heap
  (
  RandomAccessIterator first,
  RandomAccessIterator last,
  Compare compare
  );
  

Complexity

Time complexity is linear. Space complexity is constant.

Example <ospace/osstd/examples/mkheap0.cpp>
#include <iostream>
#include <algorithm>

int numbers[ 6 ] = { 5, 10, 4, 13, 11, 19 };

void
main()
  {
  make_heap( numbers, numbers + 6 );

  for ( int i = 6; i >= 1; i--)
    {
    cout << numbers[ 0 ] << "\n";
    pop_heap( numbers, numbers + i );
    }
  }

19
13
11
10
5
4

Example <ospace/osstd/examples/mkheap1.cpp>
#include <iostream>
#include <algorithm>
#include <functional>

int numbers[ 6 ] = { 5, 10, 4, 13, 11, 19 };

void
main()
  {
  make_heap( numbers, numbers + 6, greater< int >() );

  for ( int i = 6; i >= 1; i--)
    {
    cout << numbers[ 0 ] << "\n";
    pop_heap( numbers, numbers + i, greater< int >() );
    }
  }

4
5
10
11
13
19


Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.