今天学习了一下优先队列的使用,使用方法和queue很类似,有top(),pop(),push(),empty()等操作。
优先队列是按照某一优先顺序,有选择地取出元素的一种数据结构,使用时要包含queue头文件。以下是一段测试代码。
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct Test
{
int x,y,z;
Test(int xx,int yy,int zz):x(xx),y(yy),z(zz) {}
};
bool operator <(const Test &a,const Test &b)
{
return a.z>b.z;//优先取z最小的Test对象
}
int main()
{
priority_queue<Test> pq;
pq.push(Test(1,2,3));
pq.push(Test(2,3,0));
pq.push(Test(4,5,6));
pq.push(Test(1,2,1));
while(!pq.empty())
{
Test temp=pq.top();
pq.pop();
cout<<temp.x<<" "<<temp.y<<" "<<temp.z<<endl;
}
return 0;
}
输出结果如下
2 3 0
1 2 1
1 2 3
4 5 6
程序优先选择z值最小的Test的对象。
下面是运算符重载的知识
http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/12/2136598.html