c++ queue的使用


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

 

 

 

 

 

VS2008中queue單向隊列的源代碼

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

[cpp]  view plain  copy
 
  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.     typedef typename _Container::value_type value_type;  
  8.     typedef typename _Container::size_type size_type;  
  9.     typedef typename _Container::reference reference;  
  10.     typedef typename _Container::const_reference const_reference;  
  11.   
  12.     queue() : c()  
  13.     {   // construct with empty container  
  14.     }  
  15.   
  16.     explicit queue(const _Container& _Cont) : c(_Cont)  
  17.     {   // construct by copying specified container  
  18.     }  
  19.   
  20.     bool empty() const  
  21.     {   // test if queue is empty  
  22.         return (c.empty());  
  23.     }  
  24.   
  25.     size_type size() const  
  26.     {   // return length of queue  
  27.         return (c.size());  
  28.     }  
  29.   
  30.     reference front()  
  31.     {   // return first element of mutable queue  
  32.         return (c.front());  
  33.     }  
  34.   
  35.     const_reference front() const  
  36.     {   // return first element of nonmutable queue  
  37.         return (c.front());  
  38.     }  
  39.   
  40.     reference back()  
  41.     {   // return last element of mutable queue  
  42.         return (c.back());  
  43.     }  
  44.   
  45.     const_reference back() const  
  46.     {   // return last element of nonmutable queue  
  47.         return (c.back());  
  48.     }  
  49.   
  50.     void push(const value_type& _Val)  
  51.     {   // insert element at beginning  
  52.         c.push_back(_Val);  
  53.     }  
  54.   
  55.     void pop()  
  56.     {   // erase element at end  
  57.         c.pop_front();  
  58.     }  
  59.   
  60.     const _Container& _Get_container() const  
  61.     {   // get reference to container  
  62.         return (c);  
  63.     }  
  64.   
  65. protected:  
  66.     _Container c;   // the underlying container  
  67. };</span>  

可以看出,由於queue只是進一步封裝別的數據結構,並提供自己的接口,所以代碼非常簡潔,如果不指定容器,默認是用deque來作為其底層數據結構的(對deque不是很了解?可以參閱《STL系列之一deque雙向隊列》)。下面給出單向隊列的使用范例:

[cpp]  view plain  copy
 
  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. using namespace std;  
  8.   
  9. int main()  
  10. {  
  11.     //可以使用list作為單向隊列的容器,默認是使用deque的。  
  12.     queue<int, list<int>> a;  
  13.     queue<int>        b;  
  14.     int i;  
  15.   
  16.     //壓入數據  
  17.     for (i = 0; i < 10; i++)  
  18.     {  
  19.         a.push(i);  
  20.         b.push(i);  
  21.     }  
  22.   
  23.     //單向隊列的大小  
  24.     printf("%d %d\n", a.size(), b.size());  
  25.   
  26.     //隊列頭和隊列尾  
  27.     printf("%d %d\n", a.front(), a.back());  
  28.     printf("%d %d\n", b.front(), b.back());  
  29.   
  30.     //取單向隊列項數據並將數據移出單向隊列  
  31.     while (!a.empty())  
  32.     {  
  33.         printf("%d ", a.front());  
  34.         a.pop();  
  35.     }  
  36.     putchar('\n');  
  37.   
  38.     while (!b.empty())  
  39.     {  
  40.         printf("%d ", b.front());  
  41.         b.pop();  
  42.     }  
  43.     putchar('\n');  
  44.     return 0;  
  45. }  

 

轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/6950917

 


免責聲明!

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



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