1. 程式人生 > >STL(3)之單項佇列(queue)

STL(3)之單項佇列(queue)

queue單向佇列與有點類似,一個是在同一端存取資料(棧),另一個是在一端存入資料,另一端取出資料。單向佇列中的資料是先進先出(First In First Out,FIFO)。在STL中,單向佇列也是以別的容器作為底部結構,再將介面改變,使之符合單向佇列的特性就可以了。因此實現也是非常方便的。下面就給出單向佇列的函式列表和VS2008中單向佇列的原始碼。單向佇列一共6個常用函式(front()、back()、push()、pop()、empty()、size()),與的常用函式較為相似。

VS2008中queue單向佇列的原始碼

友情提示:初次閱讀時請注意其實現思想,不要在細節上浪費過多的時間。

  1. <span style=
    "font-size:18px;">//VS2008中 queue的定義 MoreWindows整理(http://blog.csdn.net/MoreWindows)
  2. template<class _Ty, class _Container = deque<_Ty> >  
  3. class queue  
  4. {   // FIFO queue implemented with a container
  5. public:  
  6.     typedef _Container container_type;  
  7.     typedeftypename _Container::value_type value_type;  
  8.     typedeftypename _Container::size_type size_type;  
  9.     typedeftypename _Container::reference reference;  
  10.     typedeftypename _Container::const_reference const_reference;  
  11.     queue() : c()  
  12.     {   // construct with empty container
  13.     }  
  14.     explicit queue(const _Container& _Cont) : c(_Cont)  
  15.     {   // construct by copying specified container
  16.     }  
  17.     bool empty() const
  18.     {   // test if queue is empty
  19.         return (c.empty());  
  20.     }  
  21.     size_type size() const
  22.     {   // return length of queue
  23.         return (c.size());  
  24.     }  
  25.     reference front()  
  26.     {   // return first element of mutable queue
  27.         return (c.front());  
  28.     }  
  29.     const_reference front() const
  30.     {   // return first element of nonmutable queue
  31.         return (c.front());  
  32.     }  
  33.     reference back()  
  34.     {   // return last element of mutable queue
  35.         return (c.back());  
  36.     }  
  37.     const_reference back() const
  38.     {   // return last element of nonmutable queue
  39.         return (c.back());  
  40.     }  
  41.     void push(const value_type& _Val)  
  42.     {   // insert element at beginning
  43.         c.push_back(_Val);  
  44.     }  
  45.     void pop()  
  46.     {   // erase element at end
  47.         c.pop_front();  
  48.     }  
  49.     const _Container& _Get_container() const
  50.     {   // get reference to container
  51.         return (c);  
  52.     }  
  53. protected:  
  54.     _Container c;   // the underlying container
  55. };</span>  

可以看出,由於queue只是進一步封裝別的資料結構,並提供自己的介面,所以程式碼非常簡潔,如果不指定容器,預設是用deque來作為其底層資料結構的(對deque不是很瞭解?可以參閱《STL系列之一deque雙向佇列》)。下面給出單向佇列的使用範例:

  1. //單向佇列 queue支援 empty() size() front() back() push() pop()
  2. //By MoreWindows(http://blog.csdn.net/MoreWindows)
  3. #include <queue>
  4. #include <vector>
  5. #include <list>
  6. #include <cstdio>
  7. usingnamespace std;  
  8. int main()  
  9. {  
  10.     //可以使用list作為單向佇列的容器,預設是使用deque的。
  11.     queue<int, list<int>> a;  
  12.     queue<int>        b;  
  13.     int i;  
  14.     //壓入資料
  15.     for (i = 0; i < 10; i++)  
  16.     {  
  17.         a.push(i);  
  18.         b.push(i);  
  19.     }  
  20.     //單向佇列的大小
  21.     printf("%d %d\n", a.size(), b.size());  
  22.     //佇列頭和佇列尾
  23.     printf("%d %d\n", a.front(), a.back());  
  24.     printf("%d %d\n", b.front(), b.back());  
  25.     //取單向佇列項資料並將資料移出單向佇列
  26.     while (!a.empty())  
  27.     {  
  28.         printf("%d ", a.front());  
  29.         a.pop();  
  30.     }  
  31.     putchar('\n');  
  32.     while (!b.empty())  
  33.     {  
  34.         printf("%d ", b.front());  
  35.         b.pop();  
  36.     }  
  37.     putchar('\n');  
  38.     return 0;  
  39. }