Standard C++ Library Class Reference

Description
A heap is a particular organization of elements in a range between two random access iterators
[a, b). Its two key properties are:
*a is the largest element in the range.1.
*a may be removed by pop_heap(), or a new element added by push_heap(), in O(logN)
time.
2.
These properties make heaps useful as priority queues.
The sort_heap algorithm converts a heap into a sorted collection over the range [first, last)
using either the default operator (<) or the comparison function supplied with the algorithm.
Note that sort_heap is not stable, i.e., the elements may not be in the same relative order after
sort_heap is applied.
Complexity
sort_heap performs at most NlogN comparisons where N is equal to last - first.
Example
//
// heap_ops.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main(void)
{
int d1[4] = {1,2,3,4};
int d2[4] = {1,3,2,4};
// Set up two vectors
vector<int> v1(d1,d1 + 4), v2(d2,d2 + 4);
// Make heaps
make_heap(v1.begin(),v1.end());
make_heap(v2.begin(),v2.end(),less<int>());
// v1 = (4,x,y,z) and v2 = (4,x,y,z)
// Note that x, y and z represent the remaining
// values in the container (other than 4).
// The definition of the heap and heap operations
// does not require any particular ordering
// of these values.
// Copy both vectors to cout
ostream_iterator<int> out(cout," ");