priority_queue的用法!

//学习于此博客:http://kmplayer.javaeye.com/blog/693965

#include<iostream>
#include<queue>
using namespace std;
/*/
less<class T>    这是按值大的优先 greater<class T>这是按值小的优先
int main()
{
    priority_queue<int>tt;//默认情况下,较大值有较高的优先权
    for(int i=0;i<10;i++)
        tt.push (rand()%10);
    while(!tt.empty ())
    {
        cout<<tt.top ()<<"  ";
        tt.pop ();
    }
    cout<<endl;
}
*

/*//
int main()
{
    priority_queue<int ,vector<int >,greater<int >    >tt;//加入了greater,较小值有较高的优先权
    for(int i=0;i<10;i++)                                                 //这边的连续'> >'要与左移区分开!
        tt.push (rand()%10);
    while(!tt.empty ())
    {
        cout<<tt.top ()<<"  ";
        tt.pop ();
    }
    cout<<endl;
}*/



/*///
class node
{
public:
    int first,second;//两个参数的优先队列
    node(int a=0,int b=0):
    first(a),second(b){}
};
bool operator<(node a,node b)//重载'<'符号
{
    if(a.first ==b.first ) return a.second >b.second ;
    return a.first >b.first ;
}
*/

class node
{
public:
    int first,second;
    node(int a,int b)
    {
        first=a;second=b;
    }
    friend bool operator<(const node &x,const node &y)
    {
        if(x.first> y.first )return true;
        if(x.first ==y.first )
            if (x.second>y.second)
                return true;
        return false;
    }
    friend ostream& operator<<(ostream &os,const node &td)
    {
        return os<<td.first <<"  "<<td.second <<endl;
    }
};
int main()
{
    priority_queue<node>tt;
    tt.push (node(8,2));
    tt.push(node(2,3));
    while(!tt.empty ())
    {
        cout<<tt.top ()<<endl;

        cout<<tt.top ().first <<"   "<<tt.top ().second <<endl;
        tt.pop();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值