• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 62  文章 - 96  trackbacks - 0
            <2006年10月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(7)

            隨筆分類(66)

            隨筆檔案(62)

            文章分類(31)

            文章檔案(32)

            友情鏈接

            最新隨筆

            積分與排名

            • 積分 - 235120
            • 排名 - 108

            最新評論

            閱讀排行榜

            評論排行榜

            在優(yōu)先隊列中,優(yōu)先級高的元素先出隊列。
            標準庫默認使用元素類型的<操作符來確定它們之間的優(yōu)先級關(guān)系。
            優(yōu)先隊列的第一種用法,也是最常用的用法:

            priority_queue<int> qi;

            通過<操作符可知在整數(shù)中元素大的優(yōu)先級高。
            故示例1中輸出結(jié)果為:9 6 5 3 2

            第二種方法:
            在示例1中,如果我們要把元素從小到大輸出怎么辦呢?
            這時我們可以傳入一個比較函數(shù),使用functional.h函數(shù)對象作為比較函數(shù)。

            priority_queue<int, vector<int>, greater<int> >qi2;

            其中
            第二個參數(shù)為容器類型。
            第二個參數(shù)為比較函數(shù)。
            故示例2中輸出結(jié)果為:2 3 5 6 9

            第三種方法:
            自定義優(yōu)先級。

            struct node
            {
                
            friend bool operator< (node n1, node n2)
                {
                    
            return n1.priority < n2.priority;
                }
                
            int priority;
                
            int value;
            };

            在該結(jié)構(gòu)中,value為值,priority為優(yōu)先級。
            通過自定義operator<操作符來比較元素中的優(yōu)先級。
            在示例3中輸出結(jié)果為:
            優(yōu)先級  值
            9          5
            8          2
            6          1
            2          3
            1          4
            但如果結(jié)構(gòu)定義如下:

            struct node
            {
                
            friend bool operator> (node n1, node n2)
                {
                    
            return n1.priority > n2.priority;
                }
                
            int priority;
                
            int value;
            };

            則會編譯不過(G++編譯器)
            因為標準庫默認使用元素類型的<操作符來確定它們之間的優(yōu)先級關(guān)系。
            而且自定義類型的<操作符與>操作符并無直接聯(lián)系,故會編譯不過。

            //代碼清單

            #include<iostream>
            #include
            <functional>
            #include
            <queue>
            using 
            namespace std;
            struct node
            {
                
            friend bool operator< (node n1, node n2)
                {
                    
            return n1.priority < n2.priority;
                }
                
            int priority;
                
            int value;
            };
            int main()
            {
                
            const int len = 5;
                
            int i;
                
            int a[len= {3,5,9,6,2};
                
            //示例1
                priority_queue
            <int> qi;
                
            for(i = 0; i < len; i++)
                    qi.push(a[i]);
                
            for(i = 0; i < len; i++)
                {
                    cout
            <<qi.top()<<" ";
                    qi.pop();
                }
                cout
            <<endl;
                
            //示例2
                priority_queue
            <int, vector<int>, greater<int> >qi2;
                
            for(i = 0; i < len; i++)
                    qi2.push(a[i]);
                
            for(i = 0; i < len; i++)
                {
                    cout
            <<qi2.top()<<" ";
                    qi2.pop();
                }
                cout
            <<endl;
                
            //示例3
                priority_queue
            <node> qn;
                node b[
            len];
                b[
            0].priority = 6; b[0].value = 1
                b[
            1].priority = 9; b[1].value = 5
                b[
            2].priority = 2; b[2].value = 3
                b[
            3].priority = 8; b[3].value = 2
                b[
            4].priority = 1; b[4].value = 4

                
            for(i = 0; i < len; i++)
                    qn.push(b[i]);
                cout
            <<"優(yōu)先級"<<'\t'<<"值"<<endl;
                for(i = 0; i < len; i++)
                {
                    cout
            <<qn.top().priority<<'\t'<<qn.top().value<<endl;
                    qn.pop();
                }
                
            return 0;
            }
            posted on 2007-04-06 02:09 beyonlin 閱讀(57168) 評論(4)  編輯 收藏 引用 所屬分類: C++之路

            FeedBack:
            # re: 優(yōu)先隊列用法 2011-03-08 19:57 Sticktotheend
            頂  回復(fù)  更多評論
              
            # re: 優(yōu)先隊列用法 2014-04-30 09:56 ***
            給力!!  回復(fù)  更多評論
              
            # re: 優(yōu)先隊列用法 2015-05-14 16:13 tanti
            久久精品国产亚洲77777| 大香伊人久久精品一区二区| 国产精品久久婷婷六月丁香| 中文字幕亚洲综合久久2| 中文精品久久久久国产网址| 亚洲AV无码久久精品蜜桃| 国产精品美女久久久久网| 国产精久久一区二区三区| 伊色综合久久之综合久久| 精品国产乱码久久久久久呢| 777米奇久久最新地址| 久久综合噜噜激激的五月天| 日本精品久久久久中文字幕8 | 久久综合九色综合精品| 中文精品久久久久国产网址| 色综合久久综精品| 久久久久18| 亚洲精品无码成人片久久| 久久久精品午夜免费不卡| 999久久久国产精品| 欧美精品乱码99久久蜜桃| 久久久一本精品99久久精品66| 久久久久免费看成人影片| 日本免费一区二区久久人人澡| 久久99热这里只有精品国产 | 久久影院久久香蕉国产线看观看| 精品无码久久久久国产| 色播久久人人爽人人爽人人片aV| 久久久久国产精品| 精品久久久久国产免费| 久久久久国产精品嫩草影院| 久久国产一区二区| 久久久精品人妻一区二区三区四| 久久精品国产一区二区三区日韩| 人妻无码久久精品| 久久水蜜桃亚洲av无码精品麻豆 | 久久综合色之久久综合| 91精品国产91久久综合| 综合久久精品色| 热RE99久久精品国产66热| 国产午夜福利精品久久2021 |