如何在结构体中重载运算符,使得优先队列可以直接按照指定顺序进行排序?
struct cmp1{
bool operator ()(int &a,int &b){
return a>b;//最小值优先
}
};
struct cmp2{
bool operator ()(int &a,int &b){
return a<b;//最大值优先
}
};
//定义结构,使用运算符重载,自定义优先级2
struct number1{
int x;
bool operator < (const number1 &a) const {
return x>a.x;//最小值优先
}
};
struct number2{
int x;
bool operator < (const number2 &a) const {
return x<a.x;//最大值优先
}
};
priority_queue<int>que;//采用默认优先级构造队列
priority_queue<int,vector<int>,cmp1>que1;//最小值优先
priority_queue<int,vector<int>,cmp2>que2;//最大值优先
priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
//这是右移运算符,所以这里用空格号隔开
priority_queue<int,vector<int>,less<int> >que4;最大值优先
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
struct node
{
int id;
int lev;
bool operator < (const node & a) const
{
if(a.lev == lev) return a.id < id;
else return a.lev > lev;
}
};
priority_queue<node> q[4];
int main()
{
int n;
char a[5];
node tmp;
int id;
while(scanf("%d",&n) != EOF)
{
id = 0;
for(int i = 1; i <= 3; i ++)
{
while(! q[i].empty())
q[i].pop();
}
int x, y;
for(int i = 1; i <= n; i ++)
{
scanf("%s",a);
if(a[0] == 'I')
{
scanf("%d%d",&x,&y);
tmp.id = ++ id;
tmp.lev = y;
q[x].push(tmp);
}
if(a[0] == 'O')
{
scanf("%d",&x);
if(q[x].empty())
{
printf("EMPTY\n");
}
else
{
tmp = q[x].top();
printf("%d\n",tmp.id);
q[x].pop();
}
}
}
}
return 0;
}