有关c++ priority_queue的问题

2025-01-31 06:35:54
推荐回答(1个)
回答1:

利用函数对象,先定义priority_queue的排序规则,
可以继承binary_function并重写operator(),然后如你的程序所写就可以了,
程序如下:

#include
#include
#include "point.h"
#include
using namespace std;

class PointCmp : public binary_function
{
public:
bool operator()(point* p1, point* p2) const
{
return p1->y < p2->y;
}

};

void main()
{
point* a = new point(1,2,3);
point* b = new point(2,3,4);
point* c = new point(3,4,5);

priority_queue, PointCmp> q ;

q.push(b);
q.push(c);
q.push(a);

point* current;
while (!q.empty())
{
current = q.top();
q.pop();
(current->y)--;
if (current->y>0)
{
q.push(current);
}
cout<x< }

}

头文件未改动