一,概述
先进先出的数据结构,底端加入元素,顶端移除元素,类似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();
- }
- }
本文详细介绍了队列这种先进先出的数据结构,包括其基本概念、常用操作方法如push、pop等,并通过示例展示了如何使用C++标准库中的queue容器适配器实现队列,适用于初学者和开发者快速掌握队列的应用。
260

被折叠的 条评论
为什么被折叠?



