c++优先级队列实现小值优先方式1

2018-09-23  本文已影响1人  aajk

#include <iostream>

using namespace std;

#include "queue"

class apple{

friend bool operator<(const apple &obj1,const apple &obj2){

return obj1.a>obj2.a;

}

public:

int a;

public:

apple(int a){

this->a = a;

}

};

void main(){

priority_queue<apple> p;

p.push(apple(10));

p.push(apple(9));

p.push(apple(8));

while (!p.empty()){

cout << p.top().a << endl;

p.pop();

}

system("pause");

}

说明:优先级队列判断优先级的依据是全局函数operator<()函数,只需要修改就行了,注意一定要写成全局函数的形式。

上一篇 下一篇

猜你喜欢

热点阅读