//priority_queue 优先队列
//队列中最大的元素总是位于队首,出队时先出最大的元素 (优先队列是 从大到小 排列)(可以重载 < 重新定义比较规则)
#include<bits/stdc++.h>
using namespace std;
priority_queue<int> pq;
int main()
{
pq.push(1); //入队
int n=pq.size(); //返回队列中的数目
pq.empty(); //判断队列是否为空
int a=pq.top(); //返回队首元素
pq.pop(); //出队(队首元素)
/*重载 < 运算符来定义优先级 (优先队列的元素类型是结构体)
struct inf
{
string name;
float score;
bool operator < (const inf &a) const
{
//按score从小到大排列。如果要从大到小,变成 >
return a.score < score;
}
}
/*重载 () 运算符来定义优先级(优先队列的元素类型是非结构体 或 结构体)
struct myCOMP
{
bool operator () (const int &a, const int &b) (函数内部的判断是 a<b 看的返回值)
{
由小到大排列用 > 号, 有从大到小用 < 号
return a>b;
}
}
*/
return 0;
}
STL常用函数复习之————priority_queue
最新推荐文章于 2024-09-08 23:04:51 发布