STL系列之五 priority_queue 优先级队列

priority_queue 优先级队列是一个拥有权值概念的单向队列queue,在这个队列中,所有元素是按优先级排列的(也可以认为queue是个按进入队列的先后做为优先级的优先级队列——先进入队列的元素优先权要高于后进入队列的元素)。在计算机操作系统中,优先级队列的使用是相当频繁的,进线程调度都会用到。在STL的具体实现中,priority_queue也是以别的容器作为底部结构,再根据堆的处理规则来调整元素之间的位置。

priority_queue函数列表
函数描述      by MoreWindows( http://blog.youkuaiyun.com/MoreWindows )
构造析构 
priority_queue <Elem> c 创建一个空的queue 。
注:priority_queue构造函数有7个版本,请查阅MSDN
数据访问与增减 
c.top()返回队列头部数据
c.push(elem)在队列尾部增加elem数据
 c.pop()队列头部数据出队
其它操作 
c.empty()判断队列是否为空
c.size()

返回队列中数据的个数

  

可以看出priority_queue的函数列表与栈stack的函数列表是相

template<class _Ty, class _Container = vector<_Ty>, class _Pr = less<typename _Container::value_type> > //默认以vector为容器的

class priority_queue

{	// priority queue implemented with a _Container

public:

	typedef _Container container_type;

	typedef typename _Container::value_type value_type;

	typedef typename _Container::size_type size_type;

	typedef typename _Container::reference reference;

	typedef typename _Container::const_reference const_reference;

 

	priority_queue() : c(), comp()

	{	// construct with empty container, default comparator

	}

 

	explicit priority_queue(const _Pr& _Pred) : c(), comp(_Pred)

	{	// construct with empty container, specified comparator

	}

 

	priority_queue(const _Pr& _Pred, const _Container& _Cont) : c(_Cont), comp(_Pred)

	{	// construct by copying specified container, comparator

		make_heap(c.begin(), c.end(), comp); //参见《STL系列之四 heap 堆的相关函数》

	}

 

	template<class _Iter>

	priority_queue(_Iter _First, _Iter _Last) : c(_First, _Last), comp()

	{	// construct by copying [_First, _Last), default comparator

		make_heap(c.begin(), c.end(), comp);

	}

 

	template<class _Iter>

	priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred) : c(_First, _Last), comp(_Pred)

	{	// construct by copying [_First, _Last), specified comparator

		make_heap(c.begin(), c.end(), comp);

	}

 

	template<class _Iter>

	priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred, const _Container& _Cont) : c(_Cont), comp(_Pred)

	{	// construct by copying [_First, _Last), container, and comparator

		c.insert(c.end(), _First, _Last);

		make_heap(c.begin(), c.end(), comp);

	}

 

	bool empty() const

	{	// test if queue is empty

		return (c.empty());

	}

 

	size_type size() const

	{	// return length of queue

		return (c.size());

	}

 

	const_reference top() const

	{	// return highest-priority element

		return (c.front());

	}

 

	reference top()

	{	// return mutable highest-priority element (retained)

		return (c.front());

	}

 

	void push(const value_type& _Pred)

	{	// insert value in priority order

		c.push_back(_Pred);

		push_heap(c.begin(), c.end(), comp);

	}

 

	void pop()

	{	// erase highest-priority element

		pop_heap(c.begin(), c.end(), comp);

		c.pop_back();

	}

 

protected:

	_Container c;	// the underlying container

	_Pr comp;	// the comparator functor

};

同的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值