class priority_queue 简单介绍
生活随笔
收集整理的這篇文章主要介紹了
class priority_queue 简单介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今日發現要使用堆,然后priority_queue 使用的恰好是堆,默認是大根堆,這樣的話,如果遇到需要用到大根堆,小根堆來處理問題的時候,可以使用這個結構。
常用方法與隊列差不 push(),pop(),top()
上一部分代碼,可以看出默認比較是 less 所以是大根堆,默認的話,里面的容器是vector
template<class _Ty,class _Container = vector<_Ty>,class _Pr = less<typename _Container::value_type> >class priority_queue{ // priority queue implemented with a _Container public:typedef priority_queue<_Ty, _Container, _Pr> _Myt;typedef _Container container_type;typedef typename _Container::value_type value_type;protected:_Container c; // the underlying container_Pr comp; // the comparator functor 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);}void push(value_type&& _Val){ // insert element at beginning c.push_back(_STD move(_Val));push_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());}void push(const value_type& _Val){ // insert value in priority order c.push_back(_Val);push_heap(c.begin(), c.end(), comp);}void pop(){ // erase highest-priority element pop_heap(c.begin(), c.end(), comp);c.pop_back();}void swap(_Myt& _Right){ // exchange contents with _Right _Swap_adl(c, _Right.c);_Swap_adl(comp, _Right.comp);}};?
轉載于:https://www.cnblogs.com/cycxtz/p/4742832.html
總結
以上是生活随笔為你收集整理的class priority_queue 简单介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle Length 和 Leng
- 下一篇: GATT之Device informat