priority_queue优先级队列

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


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

1. class Type是基础数据类型

2. class Container是默认容器为Vector

3. class Compare是指排序方式,默认是less从小到大排序


下面五个函数是常用函数:

返回队列头部元素 top()

向队头插入元素 push()

删除队头元素 pop()

判断队列是否为空 empty()

返回队列元素个数 size()


priority_queue源码

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
};


实现priority_queue的实例:先增加4人进队,再增加3人。按权值从小到大,字符序列从小到大排列

#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
//结构体
struct Node
{
	char szName[20];
	int  priority;
	Node(int nri, char *pszName)
	{
		strcpy_s(szName, pszName);
		priority = nri;
	}
};

//结构体的比较方法 改写operator()
class NodeCmp
{
public:
	bool operator()(const Node &na, const Node &nb)
	{
		if (na.priority != nb.priority)
			return na.priority >= nb.priority;
		else
			return strcmp(na.szName, nb.szName) > 0;
	}
};

void PrintfNode(Node &na)
{
	printf("%s %d\n", na.szName, na.priority);
}

int main()
{
	//优先级队列默认是使用vector作容器,底层数据结构为堆。
	priority_queue<Node, vector<Node>, NodeCmp> a;

	//有4个人进入队列
	a.push(Node(5, "小谭"));
	a.push(Node(3, "小刘"));
	a.push(Node(1, "小涛"));
	a.push(Node(5, "小王"));


	//再进入3个人
	a.push(Node(2, "小白"));
	a.push(Node(2, "小强"));
	a.push(Node(3, "小新"));

	//所有人都依次出队
	while (!a.empty())
	{
		PrintfNode(a.top());
		a.pop();
	}

	return 0;
}

以上内容可参考 MSDN



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值