自定义优先队列
priority_queue<int,vector<int>,greater<int> >que;//最小值优先
priority_queue<int,vector<int>,less<int> >que;//最大值优先
//定义结构,使用运算符重载,自定义优先级 struct cmp1{ bool operator ()(int &a,int &b){ return a>b;//最小值优先 } }; struct cmp2{ bool operator ()(int &a,int &b){ return a<b;//最大值优先 } };
priority_queue<int,vector<int>,cmp1>que1;//最小值优先
priority_queue<int,vector<int>,cmp2>que2;//最大值优先
#include<queue>
#include<utility>
#include<iostream>
using namespace std;
struct number{
int x,y;
number(int xx,int yy){
x=xx,y=yy;
}
bool operator < (const number &a)const{
return x>a.x;//最小值优先
}
};
int main()
{
priority_queue<number>que;
que.push(number(3,1));
que.push(number(1,2));
que.push(number(2,3));
while(!que.empty()){
cout<<que.top().x<<' '<<que.top().y<<endl;
que.pop();
}
}
/*输出
1 2
2 3
3 1
*/