优先队列中的友元

priority_queue <int> q:默认将队列中的数从大到小排序;

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

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

队列中可以的元素可以是结构体,一个结构体中肯定有不同参数,优先队列肯定要有一个排序的参数,所以就需要友元来定义结构体的元素的优先度;

priority_queue <tree> q:

struct tree
{
    int dj;
    int id;
    friend bool operator <(const tree x,const tree y)
    {
        if(x.dj==y.dj)
        {
            return x.id>y.id;
        }
        else
        {
            return x.dj<y.dj;
        }
    }
};

当第一个参数dj的有限度相同时,在优先队列中,id小的在队列前方;

当第一个参数dj不同时,在优先队列中,dj大的在队列前方;

priority_queue <node> q:

struct node
{
    string name;
    int sz;
    int yxj;
    int id;
    friend bool operator <(node a,node b)
    {
       if(a.yxj==b.yxj)
       {
             return a.id>b.id;
       }
       else
       {
             return a.yxj>b.yxj;
       }
    }
};

当结构体中的yxj相同时,结构体中的id小的在优先队列前方;

当结构体中的yxj不同时,结构体中的yxj小的在优先队列的前方

priority_queue <node,vector<node>,cmp> q:

struct node
{
    string name;
    int sz;
    int yxj;
    int id;
};
struct cmp
{
	bool operator( )(const node a,const node b) const
	{
		if(a.yxj==b.yxj)
       {
             return a.id>b.id;
       }
       else
       {
             return a.yxj>b.yxj;
       }
	}
}; 

构造自定义函数cmp

当结构体中的yxj相同时,结构体中的id小的在优先队列前方;

当结构体中的yxj不同时,结构体中的yxj小的在优先队列的前方。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值