STL系列之三 queue 单向队列

来源:互联网 发布:光束灯编程 编辑:程序博客网 时间:2024/04/29 23:09
 

queue单向队列与栈有点类似,一个是在同一端存取数据,另一个是在一端存入数据,另一端取出数据。单向队列中的数据是先进先出(First In First Out,FIFO)。在STL中,单向队列也是以别的容器作为底部结构,再将接口改变,使之符合单向队列的特性就可以了。因此实现也是非常方便的。下面就给出单向队列的函数列表和VS2008中单向队列的源代码。单向队列一共6个常用函数(front()、back()、push()、pop()、empty()、size()),与栈的常用函数较为相似。

 

VS2008中queue单向队列的源代码

友情提示:初次阅读时请注意其实现思想,不要在细节上浪费过多的时间。

view plaincopy to clipboardprint?
  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>  

可以看出,由于栈只是进一步封装别的数据结构,并提供自己的接口,所以代码非常简洁,如果不指定容器,默认是用deque来作为其底层数据结构的(对deque不是很了解?可以参阅《STL系列之一deque双向队列》)。下面给出单向队列的使用范例:

view plaincopy to clipboardprint?
  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. }  
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 火车晚点耽误了转车怎么办 坐火车联系不上怎么办 号码被别人注册了12306怎么办 注册12306的号码换了怎么办 12306号码被注册了怎么办 12306身份证被注册了怎么办 12306被别人注册了怎么办 铁路1236注册名忘记了怎么办 12306手机被别人注册了怎么办 12306注册手机不用了怎么办 到站后火车票掉了怎么办 在手机上买了票怎么办 智行火车票抢不到票怎么办 高铁买票票丢了怎么办 异地恋房费太贵怎么办 高铁票车上丢了怎么办 取了高铁票丢了怎么办 高铁买了学生票没带学生证怎么办 买的学生票超过区间怎么办 买了超过区间的学生票怎么办 火车票大于学生票购买区间怎么办 买了学生票学生证丢了怎么办 取票学生证没带怎么办 学生卡的失磁怎么办 学生证没有充磁买不了学生票怎么办 买学生票不在优惠区间怎么办 火车票学生优惠次数用完怎么办 学生乘火车优惠磁卡丢了怎么办 磁卡锁的卡丢了怎么办 电梯磁卡扣丢了怎么办 买火车票手机号填错了怎么办 买火车票乘客身份核验失败怎么办 火车票不在一个车厢怎么办站票 坐火车丢东西了怎么办 g2坐过站了怎么办 坐火车买近了怎么办 打印的纸质火车票丢失怎么办 格力空调没保修单怎么办 格力空调不兑现保修怎么办 哈空调如果退市怎么办 空调保修单丢了怎么办