优先队列

头文件: #include <queue>

声明方法:priority_queue <int> a;   // int 为 a 的类型

最常用的几个基本操作:

empty( )  //判断一个队列是否为空

pop( )  //删除队顶元素

top( )  //返回优先队列的队顶元素

push( )  //加入一个元素

size( )  //返回优先队列中拥有的元素个数

关于优先队列的排序:

①默认排序,从大到小;

定义方法就是 

priority_queue <int> a;

②从小到大排序

priority_queue<int,vector<int>,greater<int> > a ;

③自定义排序

(有好几种,这里只记录一个)

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct node {
    int a,b;
    friend bool operator < (node x,node y){
        if (x.a!=y.a)
            return x.a>y.a;   // 这里是按照从小到大排序
        return x.b>y.b;
    }
}b;
priority_queue<node> a;
int main ()
{
    int n,m,i,t,j,k;
    scanf("%d",&n);
    for (i=0;i<n;++i)
    {
        scanf("%d %d",&b.a,&b.b);
        a.push(b);
    }
    while(!a.empty())
  {
        printf("%d %d
",a.top().a , a.top().b );
        a.pop();
    }
    return 0;
}

运行结果:

原文地址:https://www.cnblogs.com/blowhail/p/11162336.html