一,概述
先进先出的数据结构,底端加入元素,顶端移除元素,类似stack同样不能有遍历行为,没有迭代器。也是以既有容器为底端容器被归类为陪接器(container adapter),默认底端容器为deque。
二,使用
#include <queue>
using namespace std;
三,方法
queue::push( ); //底部插入元素
queue::pop( ); //顶端移除元素
queue::empty( ); //是否为空
queue::back( );
queue::front( );
queue::size( );
四,示例
- #include<list>
- #include<iostream>
- #include<queue>
- #include<deque>
- usingnamespacestd;
- //Usingqueuewithlist
- typedeflist<int>INTLIST;
- typedefqueue<int,INTLIST>INTQUEUE;
- //Usingqueuewithdeque
- typedefdeque<constchar*>CHARDEQUE;
- typedefqueue<constchar*,CHARDEQUE>CHARQUEUE;
- intmain(void)
- {
- size_tsize_q;
- INTQUEUEq;
- CHARQUEUEp;
- //Insertitemsinthequeue(useslist)
- q.push(42);
- q.push(100);
- q.push(49);
- q.push(201);
- size_q=q.size();
- cout<<"sizeofqis:"<<size_q<<endl;
- while(!q.empty())
- {
- cout<<q.front()<<"";
- q.pop();
- }
- p.push("cat");
- p.push("ape");
- p.push("dog");
- p.push("mouse");
- p.push("horse");
- cout<<"\n"<<"p.back:"<<p.back()<<endl;//thelastelemet
- size_q=p.size();
- cout<<"sizeofpis:"<<size_q<<endl;
- while(!p.empty())
- {
- cout<<p.front()<<"";
- p.pop();
- }
- }