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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

[STL]priority_queue

發布時間:2024/4/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [STL]priority_queue 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[STL]priority_queue


由于merge k sorted lists要用到優先隊列,所以參看各種博客。現在總結一下。

按默認規定,priority_queue簡單地用<<script type="math/tex" id="MathJax-Element-3"><</script>運算符做元素比較。

根據這里:

class mycomparison {bool reverse;public:mycomparison(const bool& revparam=false){reverse=revparam;}bool operator() (const int& lhs, const int&rhs) const{if (reverse) return (lhs>rhs);else return (lhs<rhs);} };void testPQ() {int myints[]= {10,60,50,20};priority_queue<int> first;priority_queue<int> second (myints,myints+4);priority_queue<int, std::vector<int>, std::greater<int> >third (myints,myints+4);// using mycomparison:typedef std::priority_queue<int,vector<int>,mycomparison> mypq_type;mypq_type fourth(myints,myints+4); // less-than comparisonmypq_type fifth (myints,myints+4, mycomparison(true)); // greater-than comparison//emptycout << "first: " << endl;while (!first.empty()){cout << first.top() << " ";first.pop();}cout << endl;//default maxHeapcout << "second: " << endl;while (!second.empty()){cout << second.top() << " ";second.pop();}cout << endl;//minHeapcout << "third: " << endl;while (!third.empty()){cout << third.top() << " ";third.pop();}cout << endl;//less than maxHeap-DIYcout << "fourth: " << endl;while (!fourth.empty()){cout << fourth.top() << " ";fourth.pop();}cout << endl;//greater than minHeap-DIYcout << "fifth: " << endl;while (!fifth.empty()){cout << fifth.top() << " ";fifth.pop();}cout << endl; }

程序的輸出結果:

first為空,second默認為最大堆,third用greater(要include <functional>)變成最小堆,fourth和fifth用的都是自定義的比較函數,mycomparison是一個類,重載operator()。如果參數為true,即lhs>rhs,即greater,為最小堆。

對于自定義的類型,也同樣可以插入優先隊列。比如現在要把

struct Node {int val;Node* next;Node(int x) : val(x), next(NULL) {} };

根據val值插入到最小堆(當然更準確的描述應該是val值越小,越先到達top())。

有兩種方法:

  • 改變node結構體,重載<<script type="math/tex" id="MathJax-Element-4"><</script>,修改成:
  • struct Node {int val;Node* next;bool operator < (const Node &x) const {return val > x.val;}Node(int x) : val(x), next(NULL) {} };

    測試函數:

    void testPq() {priority_queue<Node, vector<Node>> pq;int a[6] = {5, 6, 8, 10, 230, 0};for (int i = 0; i < 6; i++)pq.push(Node(a[i]));while (!pq.empty()){Node temp = pq.top();pq.pop();cout << temp.val << " ";}cout << endl; }

    輸出為:

    2. 重新定義一個比較類。

    class nodeComparison {//此函數要加上public。//“>”說明是最小堆。“<”說明是最大堆 public: bool operator() (Node &n1, Node &n2){return n1.val > n2.val;} };

    測試函數同上。初始化priority_queue的時候有一點不同:

    priority_queue<Node, vector<Node>, nodeComparison> pq;

    如果要push進priority_queue的是Node型指針。則要定義一個比較類了:

    class nodeComparison {//此函數要加上public。//“>”說明是最小堆。“<”說明是最大堆 public: bool operator() (Node* n1, Node* n2){return n1->val > n2->val;} };

    綜上,如果是基本內置類型,可以用less,greter或者再定義一個比較類,重載operator()。如果是自定義類型,則直接定義一個比較類。The CPP Languages P414,424

    參考:
    http://www.cnblogs.com/cszlg/archive/2012/07/27/2611607.html

    總結

    以上是生活随笔為你收集整理的[STL]priority_queue的全部內容,希望文章能夠幫你解決所遇到的問題。

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