优先队列底层是用堆来实现的,队首元素一定是当前队列中优先级最高的那个元素
使用案例如下
#include <iostream>
#include <string>
#include <queue>
#include <vector>
using namespace std;
struct fruit
{
string name;
int price;
}f1,f2,f3;
struct cmp {
bool operator ()(fruit a, fruit b) {
return a.price < b.price;
}
};
int main() {
priority_queue<fruit, vector<fruit>, cmp> pquse;
f1.name = "apple";
f1.price = 4;
f2.name = "banana";
f2.price = 3;
f3.name = "orange";
f3.price = 1;
pquse.push(f1);
pquse.push(f2);
pquse.push(f3);
cout << pquse.top().name << " " << pquse.top().price << endl;
system("PAUSE");
return 0;
}
输出:
apple 4