priority_queue的基本用法

时间:2022-03-29 04:03:27
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[];
for(int i=;i<=;i++) a[i]=i;
sort(a+,a++,greater<int>() );
for(int i=;i<=;i++) cout<<a[i]<<" ";cout<<endl; // cong da dao xiao cout<<"------------------"<<endl; priority_queue<int> que; // 默认   最大之优先
for(int i=;i<=;i++) que.push(i);
while(!que.empty()) { cout<<que.top()<<" "; que.pop(); } cout<<endl; priority_queue<int,vector<int>,greater<int> >que1;//注意“>>”会被认为错误,这是右移运算符,所以这里用空格号隔开
for(int i=;i<=;i++) que1.push(i);
while(!que1.empty()) { cout<<que1.top()<<" "; que1.pop(); } cout<<endl; priority_queue<int,vector<int>,less<int> >que2;////最大值优先
for(int i=;i<=;i++) que2.push(i);
while(!que2.empty()) { cout<<que2.top()<<" "; que2.pop(); } cout<<endl;
}
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5;
int a[maxn];
int b[maxn];
struct NODE{
int x;
int y;
bool operator < (const NODE &a) const
{
if(x==a.x) return y>a.y;// zui xiao zhi you xian
return x>a.x;//最小值优先
}
};
int main()
{
for(int i=;i<=;i++) a[i]=i;
for(int i=;i<=;i++) b[i]=i*i;
priority_queue<NODE> k;
for(int i=;i<=;i++)
{
NODE c;
c.x=;
c.y=b[i];
k.push(c);
cout<<c.x<<" "<<c.y<<endl;
}
cout<<k.size()<<endl;
while(!k.empty())
{
NODE c=k.top(); cout<<c.x<<" "<<c.y<<endl;
k.pop();
} }