关于优先队列的问题
优先队列里存结构 struct{int x,y,z;}我想根据Z的大小决定优先度,怎么实现?
还有,如果队列里存的数组下标,能根据数组下标对应值的大小决定优先度吗?
程序代码:#include <queue>
#include <iostream>
using namespace std;
struct foo
{
int x, y, z;
foo( int x, int y, int z ) : x(x), y(y), z(z)
{
}
};
struct foo_comp_z
{
bool operator()( const foo& lhs, const foo& rhs ) const
{
return lhs.z < rhs.z;
}
};
int main( void )
{
std::priority_queue<foo,std::vector<foo>,foo_comp_z> q;
q.push( foo(0,0,2) );
q.push( foo(0,0,1) );
q.push( foo(0,0,3) );
for( ; !q.empty(); q.pop() )
cout << q.top().z << endl;
return 0;
}