C++ stack queue priority_element
#include<queue>
usingnamespacestd;
classT
{
public:
intx,y,z;
T(inta,intb,intc):x(a),y(b),z(c)
{
}
};
booloperator<(constT&t1,constT&t2)
{
returnt1.z<t2.z;//按照z的顺序来决定t1和t2的顺序
}
main()
{
priority_queue<T>q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while(!q.empty())
{
Tt=q.top();q.pop();
cout<<t.x<<""<<t.y<<""<<t.z<<endl;
}
return1;
}
输出结果为(注意是按照z的顺序从大到小出队的):
336
225
154
443
再看一个按照z的顺序从小到大出队的例子:
#include<iostream>
#include<queue>
usingnamespacestd;
classT
{