priority_queue的基本用法

时间:2023-01-06 17:37:09

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int a[101];
   for(int i=1;i<=10;i++) a[i]=i;
   sort(a+1,a+1+10,greater<int>() );
   for(int i=1;i<=10;i++)  cout<<a[i]<<"  ";cout<<endl; // cong da dao xiao
   
cout<<"------------------"<<endl;

    priority_queue<int> que; // 默认   最大之优先
    for(int i=1;i<=10;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=1;i<=10;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=1;i<=10;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=1;i<=10;i++) a[i]=i;
   for(int i=1;i<=10;i++) b[i]=i*i;
   priority_queue<NODE> k;
   for(int i=1;i<=10;i++)
   {
       NODE c;
       c.x=1;
       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();
   }

}