优先队列 的操作符重载 priority_queue <node> q

/*
* @Author: Achan
* @Date:   2019-03-30 23:03:36
* @Last Modified by:   Achan
* @Last Modified time: 2019-03-30 23:09:04
*/
#include<bits/stdc++.h>
using namespace std;

struct node
{
    int dis;
    int nod;
    node(){}
    node(int dis,int nod):dis(dis),nod(nod){} 

    bool operator < (const node &a ) const{   //重载操作符'<'改为'>' 大根堆优先node队列变为小根堆
        if(dis == a.dis) return nod > a.nod;  //可定义多级优先度
        return dis > a.dis;         
    }
    
};


priority_queue <int>  p;//默认大根堆
priority_queue <node> q; //重载操作符实现小根堆
priority_queue <int, vector<int>, greater<int> > Q; //非重载操作符实现小根堆
int main(void)
{
    
    cout<<"默认的大根堆优先队列"<<endl;  //最大放在队首top()
    node t;
    p.push(1);
    p.push(5);
    p.push(9);
    p.push(2);
    p.push(6);
    while(!p.empty())
    {
    	Q.push(p.top());
     	cout<<p.top()<<endl; 
     	p.pop(); 
    }
    cout<<"***************"<<endl;
    while(!Q.empty())
    {
     	cout<<Q.top()<<endl; 
     	Q.pop(); 
    }
    cout<<"***************"<<endl;

    
    cout<<"重载排序'<'逻辑的小根堆二级优先队列"<<endl;  
    t.dis = 1;
    t.nod = 5;
    q.push(t);

    t.dis = 1;
    t.nod = 1;
    q.push(t);

    t.dis = 9;
    t.nod = 5;
    q.push(t);
    
    t.dis = 2;
    t.nod = 5;
    q.push(t);
    
    t.dis = 6;
    t.nod = 5;
    q.push(t); 
    
    while(!q.empty())
    {
        cout<<q.top().dis<<" "<<q.top().nod<<endl; 
        q.pop();
    }
    
    cout<<"***************"<<endl;

    
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值