优先队列 -案例

#include<iostream>
#include<queue>

using namespace std;

//升序队列
priority_queue <int,vector<int>,greater<int> > q1;//小顶堆 
//降序队列
priority_queue <int,vector<int>,less<int> >q2;//大顶堆 

priority_queue<int>q3; //默认大顶堆 

/*greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)*/

struct cmp{//重写仿函数
   bool operator()(pair<int,int>&a,pair<int,int>&b)const{
   		if(a.first>b.first) return true;
        else return   a.second>b.second;
    }
};
 
int main(){
	q1.push(5);q1.push(6);q1.push(3);q1.push(4);
	q2.push(5);q2.push(6);q2.push(3);q2.push(4);	
	while(!q1.empty()){
		cout<<q1.top();
		q1.pop();
	}
	cout<<endl;
	while(!q2.empty()){
		cout<<q2.top();
		q2.pop();
	}
	cout<<endl;		
	priority_queue<pair<int, int>,vector<pair<int,int>>,cmp> a;//pari的比较,先比较第一个元素,第一个相等比较第二个
    pair<int, int> b(1, 2);
    pair<int, int> c(3, 1);
    pair<int, int> d(2, 5);
    a.push(d);
    a.push(c);
    a.push(b);
    while (!a.empty()) 
    {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }	
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值