• <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>
            面對現實,超越自己
            逆水行舟,不進則退
            posts - 269,comments - 32,trackbacks - 0
            C++ Queues(隊列)

            C++隊列是一種容器適配器,它給予程序員一種先進先出(FIFO)的數據結構。
            1.back() 返回一個引用,指向最后一個元素
            2.empty() 如果隊列空則返回真
            3.front() 返回第一個元素
            4.pop() 刪除第一個元素
            5.push() 在末尾加入一個元素
            6.size() 返回隊列中元素的個數

            隊列可以用線性表(list)或雙向隊列(deque)來實現(注意vector container 不能用來實現queue,因為vector 沒有成員函數pop_front!):
            queue<list<int>> q1;
            queue<deque<int>> q2;
            其成員函數有“判空(empty)” 、“尺寸(Size)” 、“首元(front)” 、“尾元(backt)” 、“加入隊列(push)” 、“彈出隊列(pop)”等操作。

            例:
            1 int main()
            2 {
            3     queue<int> q;
            4     q.push(4);
            5     q.push(5);
            6     printf("%d\n",q.front());
            7     q.pop();
            8 }

            C++ Priority Queues(優先隊列)

            C++優先隊列類似隊列,但是在這個數據結構中的元素按照一定的斷言排列有序。
            1.empty() 如果優先隊列為空,則返回真
            2.pop() 刪除第一個元素
            3.push() 加入一個元素
            4.size() 返回優先隊列中擁有的元素的個數
            5.top() 返回優先隊列中有最高優先級的元素

            優先級隊列可以用向量(vector)或雙向隊列(deque)來實現(注意list container 不能用來實現queue,因為list 的迭代器不是任意存取iterator,而pop 中用到堆排序時是要求randomaccess iterator 的!):
            priority_queue<vector<int>, less<int>> pq1; // 使用遞增less<int>函數對象排序
            priority_queue<deque<int>, greater<int>> pq2; // 使用遞減greater<int>函數對象排序
            其成員函數有“判空(empty)” 、“尺寸(Size)” 、“棧頂元素(top)” 、“壓棧(push)” 、“彈棧(pop)”等。

            例:
             1 #include <iostream>
             2 #include <queue> 
             3 using namespace std;
             4  
             5 class T {
             6 public:
             7     int x, y, z; 
             8     T(int a, int b, int c):x(a), y(b), z(c)
             9     { 
            10     }
            11 };
            12 bool operator < (const T &t1, const T &t2) 
            13 {
            14     return t1.z < t2.z; // 按照z的順序來決定t1和t2的順序
            15 
            16 main()
            17 
            18     priority_queue<T> q; 
            19     q.push(T(4,4,3)); 
            20     q.push(T(2,2,5)); 
            21     q.push(T(1,5,4)); 
            22     q.push(T(3,3,6)); 
            23     while (!q.empty()) 
            24     { 
            25         T t = q.top(); 
            26         q.pop(); 
            27         cout << t.x << " " << t.y << " " << t.z << endl; 
            28     } 
            29     return 1
            30 }
                  輸出結果為(注意是按照z的順序從大到小出隊的):
                  3 3 6
                  2 2 5
                  1 5 4
                  4 4 3

                  再看一個按照z的順序從小到大出隊的例子:
             1 #include <iostream> 
             2 #include <queue> 
             3 using namespace std; 
             4 class T 
             5 
             6 public
             7     int x, y, z; 
             8     T(int a, int b, int c):x(a), y(b), z(c) 
             9     {
            10     } 
            11 }; 
            12 bool operator > (const T &t1, const T &t2) 
            13 
            14     return t1.z > t2.z; 
            15 
            16 main() 
            17 
            18     priority_queue<T, vector<T>, greater<T> > q; 
            19     q.push(T(4,4,3)); 
            20     q.push(T(2,2,5)); 
            21     q.push(T(1,5,4)); 
            22     q.push(T(3,3,6)); 
            23     while (!q.empty()) 
            24     { 
            25         T t = q.top(); 
            26         q.pop(); 
            27         cout << t.x << " " << t.y << " " << t.z <<  endl; 
            28     } 
            29     return 1
            30 }
                  輸出結果為:
                  4 4 3
                  1 5 4
                  2 2 5
                  3 3 6
                  如果我們把第一個例子中的比較運算符重載為: bool operator < (const T &t1, const T &t2) { return t1.z > t2.z; // 按照z的順序來決定t1和t2的順序} 則第一個例子的程序會得到和第二個例子的程序相同的輸出結果。

            posted on 2012-06-05 13:21 王海光 閱讀(49365) 評論(0)  編輯 收藏 引用 所屬分類: STL
            久久久久国产| 久久精品一区二区国产| 久久九九久精品国产免费直播| 久久夜色精品国产欧美乱| 久久精品成人免费网站| 理论片午午伦夜理片久久| 人妻精品久久无码区| 久久精品女人天堂AV麻| 青青草原精品99久久精品66 | 精品久久久久久亚洲精品| 久久99热国产这有精品| 怡红院日本一道日本久久| 久久精品无码一区二区WWW | 久久久久国色AV免费看图片| 99精品久久精品一区二区| 国内精品免费久久影院| 久久国产欧美日韩精品| 免费一级欧美大片久久网| 嫩草影院久久99| 色婷婷综合久久久久中文一区二区| 久久se精品一区精品二区国产| 久久香蕉超碰97国产精品| 欧美精品乱码99久久蜜桃| 国产亚洲美女精品久久久| 996久久国产精品线观看| 一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 国产人久久人人人人爽| 久久AV无码精品人妻糸列| 欧美激情精品久久久久久久| 大蕉久久伊人中文字幕| 久久精品国产亚洲沈樵| 精品精品国产自在久久高清| av色综合久久天堂av色综合在| 热99RE久久精品这里都是精品免费| 久久性生大片免费观看性| 久久久精品国产亚洲成人满18免费网站 | 久久人人爽人人精品视频| 91精品国产高清久久久久久国产嫩草| 狼狼综合久久久久综合网| 日韩精品久久无码人妻中文字幕| 久久香综合精品久久伊人|