make_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 .
#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
);
Time complexity is linear. Space complexity is constant.
#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
#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.