【优先队列】【堆】STL之priority_queue、make_heap()、push_heap()、pop_heap()、容器适配器

本文详细介绍了STL中的优先队列priority_queue,包括其默认底层容器vector、比较规则、与make_heap等函数的关系,以及如何自定义比较操作。同时,讨论了priority_queue的构造函数参数及其作用,以及它与stack和queue的区别。
摘要由CSDN通过智能技术生成

类模板:

template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> >
class priority_queue;


构造函数:

explicit priority_queue (const Compare& comp = Compare(),
						const Container& ctnr = Container());

template <class InputIterator>
priority_queue (InputIterator first, InputIterator last,
				const Compare& comp = Compare(),
				const Container& ctnr = Container());

1. 关于构造函数的第一个参数 const Compare& comp

(1) 如果不指定,则采用类模板参数 Compare 创建的对象,而Compare 的默认值为 less(http://blog.csdn.net/duyiwuer2009/article/details/23277803)

因此,对于自定义结构,有两种方式实现比较操作:

a. 重载 "<"(即采用 less, 因为 less 重载调动操作符,比较通过 "<" 实现的)

b. 自定义 functor——重载调用操作符的类

The third template parameter supplies the means of making priority comparisons.  It defaults to @c less<value_type> but can be anything defining a strict weak ordering.

实质上,comp 被传给了底层的堆方法,见下文 priority_queue 的实现


2. priority_queue 实质上不是容器,默认底层容器为 vector

This is not a true container, but an @e adaptor.  It holds another container, and provides a wrapper interface to that container.

默认底层容器为 vector

The second template parameter defines the type of the underlying sequence/container.  It defaults tostd::vector, but it can be any type that supports @cfront(), @cpush_back, @cpop_back, and random-access iterators, such as

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值