堆priority_queue
#include<queue>
//#include<bits/stdc++.h>
using namespace std;
priority_queue<int> q;
//大根堆
//小根堆最简单的方法:取负号
struct rec
{
int a,b;
};
//如果要把结构体 放入 stl比大小 只能重载小于号
bool operator<(const rec &x,const rec &y)
{
return x.a + x.b > y.a + y.b;
}
priority_queue<rec> qq;//取出a+b最小的结构体
int main()
{
q.push(233);
q.push(2233);//向堆里面加入一个元素
q.pop();//从堆中删除一个元素 删除是堆中最大的元素 2233 void类型没有返回值
int x = q.top();//获取堆中最大元素 233
cout << q.size() << endl;//获取堆剩余的元素个数 1
}

浙公网安备 33010602011771号