STL中的优先队列——priority_queue
- 头文件
#include<queue>
- 声明和创建
priority_queue<int> q;//默认为数值大的优先
- 优先级
priority_queue<int,vector<int>,less<int> >//less<int> 数值大的优先
priority_queue<int,vector<int>,greater<int> >//greater<int> 数值小的优先
- 自定义优先级
//通过写比较器(struct)中, 重载运算符bool operator()
struct compare{
bool operator()(ListNode* p,ListNode* q){
return p->val>q->val;
}
}
priority_queue<ListNode*,vector<ListNode*>,compare>
//其中ListNode是链表的一个节点
- 常用操作
q.push(val);
q.pop();
q.top();