美文网首页
c++优先级队列实现小值优先方式1

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

作者: aajk | 来源:发表于2018-09-23 21:27 被阅读1次

#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<()函数,只需要修改就行了,注意一定要写成全局函数的形式。

相关文章

网友评论

      本文标题:c++优先级队列实现小值优先方式1

      本文链接:https://www.haomeiwen.com/subject/xjfvpftx.html