优先队列

本文详细介绍了C++中priority_queue的基本使用方法,包括最大优先队列与最小优先队列的创建方式,以及如何通过自定义类型实现优先队列。此外,还探讨了heap的相关操作函数,如make_heap, push_heap等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.priority_queue

(1)基本类型

最大优先队列:

priority_queue<int> q;

priority_queue<int,vector<int> > q; 

priority_queue<int,vector<int>,less<int> > q;

最小优先队列:

priority_queue<int,vector<int>,greater<int> > q;


http://www.cplusplus.com/reference/queue/priority_queue/


(2)自定义类型

重载:

//最大堆
struct Node
{
    int key;
};

bool operator<(Node a,Node b)
{
    return a.key<b.key;
}

priority_queue<Node> q;

仿函数:

//最大堆
struct Node
{
    int key;
};

struct cmp
{
    bool operator()(Node a,Node b)
    {
        return a.key<b.key;
    }
};

priority_queue<Node,vector<Node>,cmp> q;

//最小堆
struct Node
{
    int key;
};

struct cmp
{
    bool operator()(Node a,Node b)
    {
        return a.key>b.key;
    }
};

priority_queue<Node,vector<Node>,cmp> q;


2.heap

make_heap(),push_heap(),pop_heap(),sort_heap()


http://www.cplusplus.com/reference/algorithm/push_heap/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值