1. 主页
  2. 文档
  3. Level4题解(1-10)
  4. 第3课 队列
  5. queue

queue

使用队列,要先包含头文件 : #include<queue>

队列的主要操作:

queue <Element> q;    //定义队列queue,其中<Element>为数据类型(如 int,float,char等)
q.push(item)         //将item压入队列尾部
q.pop()             //删除队首元素,但不返回
q.front()          //返回队首元素,但不删除
q.back()           //返回队尾元素,但不删除
q.size()          //返回队列中元素的个数
q.empty()        //检查队列是否为空,如果为空返回true,否则返回false

队列操作举例

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
 
void main()
{
	queue<int> q;
	int num;
 
	cout<<"------Test for Queue-------"<<endl;
	cout<<"Input number:"<<endl;
	while(cin>>num)
	{
		q.push(num);
	}
	cout<<"Now the Queue has "<<q.size()<<" numbers."<<endl;
	cout<<"The first is "<<q.front()<<endl;
	cout<<"The last is "<<q.back()<<endl;
	cout<<"All numbers:"<<endl;
	while(!q.empty())
	{
		cout<<q.front()<<" ";
		q.pop();
	}
	cout<<"Now the Queue has "<<q.size()<<" numbers."<<endl;
	system("Pause");
 
 
}

结果截图: