优先队列的初始理解

#include<iostream>

#include<queue>

using namespace std;

struct node{

      int x;

}s;

bool operator <(node a,node b){

      return a.x<b.x;//从大到小排序,与bool cmp()sort相反

      //return a.x>b.x;//从小到大排序

}

priority_queue<node>q;

int main(){

      for(int i=1;i<=10;i++){

           cin>>s.x;//输入

           q.push(s);//入队

      }

      while(!q.empty()){

                 printf("%d ",q.top());q.pop();//输出头元素,且删除队列的第一个元素

           }

           return 0;

}

//测试数据:144 25 14 36 41587 78 558 787 552 58

#include<iostream>

#include<queue>

using namespace std;

priority_queue<int,vector<int>,less<int> >q;//less从大到小排序

int main(){

      int a;

      for(int i=1;i<=10;i++){

           cin>>a;

           q.push(a);     

      }

      while(!q.empty()){

                 printf("%d ",q.top());q.pop(); 

           }

           return 0;

}

//测试数据:1 25 658 56 56 556 565 55 99 787

#include<iostream>

#include<queue>

using namespace std;

priority_queue<int,vector<int>,greater<int> >q;//greater从小到大排序

int main(){

      int a;

      for(int i=1;i<=10;i++){

           cin>>a;

           q.push(a);     

      }

      while(!q.empty()){

                 printf("%d ",q.top());q.pop(); 

           }

           return 0;

}

//测试数据:1 25 658 56 56 556 565 55 99 787

常用方法:

优先队列默认排序为降序排列

//优先队列在普通情况下为降序排列

#include<iostream>

#include<queue>

using namespace std;

priority_queue<int>q;

int main(){

      int a;

      for(int i=1;i<=10;i++){

           cin>>a;

           q.push(a);

      }

      while(!q.empty()){

           cout<<q.top()<<" ";

           q.pop();

      }

      return 0;

}

//测试数据:4 5 14 15 1225 158 96 23 231 11

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值