优先队列

#include<queue>
#include<string>
#include<iostream>
using namespace std;
struct Fruit
{
	int price;
	string name;
}f1,f2,f3;
struct cmp
{
	bool operator()(Fruit f1,Fruit f2)
	{
		//价格越低 优先级越高
		return f1.price > f2.price;
	}
};
int main()
{
	priority_queue<Fruit, vector<Fruit>, cmp>q;
	f1.name = "桃子";
	f1.price = 10;
	f2.name = "橘子";
	f2.price = 5;
	f3.name = "梨";
	f3.price = 6;
	q.push(f1);
	q.push(f2);
	q.push(f3);

	cout << q.top().name << ":" << q.top().price;

}
  • 注:cmp中的优先级可以记为与sort的比较相反
posted @ 2021-09-03 08:00  小帆敲代码  阅读(1)  评论(0编辑  收藏  举报