#include<queue>
priority_queue与queue的不同:最大元素位于队首
priority_queue<int> pq;
pq.push(1); //入队
pq.pop(); //出队
int i = pq.top(); //获取队首元素
pq.size(); //大小
pq.empty(); //判空
如果队列元素是结构体,重载"<"操作符来修改优先性:
struct Info
{string name;float score;bool operator<(const Info& a) const{return score < a.score;}
}
struct myComp
{bool operator()(const int &a, const int &b){return a > b; //a靠近首侧,b靠近尾侧,如果为真则换位置}
}priority_queue<int, vector<int>, myComp> pq; //元素类型为int,显示说明内部结构使vector