日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

class priority_queue 简单介绍

發布時間:2024/4/17 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 简单介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。