priority_queue优先级队列

来源:互联网 发布:linux卸载软件包 编辑:程序博客网 时间:2024/05/19 04:07

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 _Containerpublic: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, comparatormake_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 comparatormake_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 comparatormake_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 comparatorc.insert(c.end(), _First, _Last);make_heap(c.begin(), c.end(), comp);}bool empty() const{// test if queue is emptyreturn (c.empty());}size_type size() const{// return length of queuereturn (c.size());}const_reference top() const{// return highest-priority elementreturn (c.front());}reference top(){// return mutable highest-priority element (retained)return (c.front());}void push(const value_type& _Pred){// insert value in priority orderc.push_back(_Pred);push_heap(c.begin(), c.end(), comp);}void pop(){// erase highest-priority elementpop_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;elsereturn 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



原创粉丝点击