在STL中,二叉堆是通过priority_queue的类模板实现的,标准头文件是<queue>。在STL中实现的是最大堆(max-heap)。
成员函数有:
void push (const Object & x);
const Object & top() const;
void pop();
bool empty();
void clear();
优先队列模版用如下参数初始化: 项类型,容器类型(几乎总是使用存储项的vector)和比较器。最后两个参数是默认值,并且默认情况下得到最大堆。使用 greater函数对象作为比较器可以得到最小堆。
示例:
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <string>
using namespace std;
//Empty the priority queue and print its contents.
template <typename PriorityQueue>
void dumpContents ( const string & msg, PriorityQueue & pq)
{
cout << msg << ":" << endl;
while (!pq.empty() )
{
cout << pq.top() << endl;
pq.