C++ 學習筆記之 STL 隊列


一.  引言

  在算法以及數據結構的實現中,很多地方我們都需要隊列(遵循FIFO,先進先出原則)。

  為了使用隊列,我們可以自己用數組來實現隊列,但自己寫太麻煩不說,並且還很容易出錯。

  好在C++的STL(標准模板庫)為我們實現了一個強大的隊列,它包含在頭文件<queue>中。

二.    queue

a)     構造函數

下面用例子來展示queue的構造函數

    deque<int> deck(3,100);

    list<int> mylist(2,100);

    queue<int> first;//默認構造

    queue<int,list<int> > second(mylist);//以list為容器構造

    queue<int> third(mylist);//以deque為容器構造,其中deque是queue的默認容器

queue<int,deque<int> > forth(mylist);//用默認容器構造,deque是queue的默認容器

我們可以使用deque(雙端隊列容器)或者list(鏈表容器)來作為queue的基礎容器(underlying container,即隊列是在基礎容器的基礎上實現的),其中deque是默認使用的,如果沒有在參數中特殊指定,那么queue就使用deque作為基礎容器。

b)     其他成員函數

  1. empty 測試容器是否為空,為空時返回true
  2. size 返回容器的大小
  3. front 返回隊列的第一個元素,即最早被壓進隊列的元素
  4. back 返回隊列的最后一個元素,即最晚被壓進隊列的元素
  5. push 把元素添加至隊列尾
  6. pop 彈出隊列首元素
  7. swap(C++11) 交換兩個隊列
  8. emplace(C++11) 在容器中直接構造元素,可以參考C++11新特性emplace操作

 

三.    priority_queue(優先隊列)

a)     構造函數

 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstdlib>
 4 #include <iostream>
 5 #include <ctime>
 6 #include <functional>
 7 using namespace std;
 8 struct Node{
 9     int a ; 
10     Node(int a):a(a){}
11 };
12 struct mycomparision{
13     bool reverse;
14     mycomparision(const bool &revparam=false){
15         reverse = revparam;
16     }
17     bool operator () (const int & lhs, const int &rhs) const {
18         if(reverse)return (lhs > rhs);//升序
19         else return (lhs < rhs);//降序
20     }
21 };
22 struct cmp{
23     bool operator () (const Node a, const Node b) const{
24         return a.a < b.a;//小於號代表降序輸出
25     }
26 };
27 int main(){
28     int myints[] = {10,60,50,20};
29     priority_queue<int> zero;
30     priority_queue<Node,vector<Node>,cmp> first;//自定義結構體的優先隊列,降序輸出
31     for(int c : myints){
32         first.push(Node(c));
33     }
34     priority_queue<int,vector<int>,mycomparision> second(myints,myints+4,mycomparision(true));//與自定義的仿函數mycomparision結合實現自定義排序功能
35     priority_queue<int,vector<int>,mycomparision> third(myints,myints+4,mycomparision());
36     priority_queue<int,vector<int>,mycomparision> forth(myints,myints+4);//輸出結果同third
37     priority_queue<int,vector<int>,less<int>> fifth(myints,myints+4);//結果同third,less使隊列優先輸出小數,此為默認,即less<int>可以省略
38     priority_queue<int,vector<int>,greater<int>> sixth(myints,myints+4);//使用functional庫中的仿函數greater使隊列優先輸出小數
39     while(!first.empty()){
40         cout << first.top().a << " ";
41         first.pop();
42     }
43     cout << endl;
44     while(!second.empty()){
45         cout << second.top() << " ";
46         second.pop();
47     }
48     cout << endl;
49     while(!third.empty()){
50         cout << third.top() << " ";
51         third.pop();
52     }
53     cout << endl;
54     while(!forth.empty()){
55         cout << forth.top() << " ";
56         forth.pop();
57     }
58     cout << endl;
59     while(!fifth.empty()){
60         cout << fifth.top() << " ";
61         fifth.pop();
62     }
63     cout << endl;
64     while(!sixth.empty()){
65         cout << sixth.top() << " ";
66         sixth.pop();
67     }
68     return 0;
69 }

 

優先隊列內部維持了堆。利用堆來實現隨機的

我們可以使用deque(雙端隊列容器)或者vector(向量容器)來作為priority_queue的基礎容器,其中vector是默認使用的,如果沒有在參數中特殊指定,那么queue就使用vector作為基礎容器。

這里還要特別注意仿函數的使用。在頭文件<functional>提供了一部分仿函數,我們可以利用這些仿函數來實現對基本數據類型的升序降序操作。但對於自定義的結構體類型,我們需要自己額外來實現仿函數來進行排序。有關仿函數的概念可以參考博客:【C++ STL】深入解析神秘的 --- 仿函數

 

b)     其他成員函數

priority_queue的成員函數與queue大體一致,其中需要注意的是:

  1. top取代了原來的front,每次取特定排序規則中的具有最值的元素
  2. 取消了back函數

以上是我自己查資料總結的隊列的一些用法,如有不對之處還望各位斧正。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM