/**
手册:http://www.cplusplus.com/reference/queue/priority_queue/
priority_queue 对于基本类型的使用方法相对简单。他的模板声明带有三个参数:
priority_queue<Type, Container, Functional>
其中Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个参数缺省的话,
优先队列就是大顶堆,队头元素最大。
*/
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct node
{
int x, y;
};
struct cmp
{
bool operator() (const node & a, const node & b)
{
if (a.x != b.x)
{
return a.x > b.x;
}
else
{
return a.y > b.y;
}
}
};
priority_queue <int, vector<int>, greater<int> > q1; //这里我想实现的是小顶堆哦~
priority_queue <node, vector<node>, cmp> q2;
int main()
{
for (int i=20; i>0; i--)
{
q1.push(i);
}
while ( !q1.empty() )
{
cout << q1.top() << " ";
q1.pop();
}
cout << endl;
node n;
for (int i=20; i>0; i--)
{
n.x = i;
n.y = i;
q2.push(n);
}
while ( !q2.empty() )
{
n = q2.top();
cout << n.x << " " << n.y << endl;
q2.pop();
}
cout << endl;
return 0;
}