链接
[https://www.cnblogs.com/xzxl/p/7266404.html]
运算符重载
2、自定义优先级:
struct cmp {
operator bool ()(int x, int y)
{
return x > y; // x小的优先级高 //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
}
};
priority_queue<int, vector<int>, cmp> q; //定义方法
//其中,第二个参数为容器类型。第三个参数为比较函数。
code
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
int main(){
priority_queue<int,vector<int>,greater<int> > p1;
priority_queue<int,vector<int>,less<int> > p2;
int n,x;
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
p1.push(x);
p2.push(x);
}
cout<<p1.size()<<endl;
while(!p1.empty()){
cout<<p1.top()<<endl;
p1.pop();
}
cout<<p2.size()<<endl;
while(!p2.empty()){
cout<<p2.top()<<endl;
p2.pop();
}
return 0;
}