基本概念
- 队列是一种特殊的线性表,是一种先进先出(FIFO)的数据结构。
- 它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。
- 进行插入操作的端称为队尾,进行删除操作的端称为队头。
- 队列中没有元素时,称为空队列。
queue常用接口
构造函数:
- queue q;//默认
- queue(const queue &que);//拷贝
赋值操作:
- queue& operator=(const queue &que);//重载等号操作符
数据存取:
- push(ele);//向队尾顶添加元素
- pop();//从队头移除一个元素
- back();//返回最后一个元素
- front();//返回第一个元素
大小操作:
- empty();//判断是否为空
- size();//返回大小
#include<iostream>
#include<queue>
using namespace std;
void test()
{
queue<int>q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
while (!empty(q))
{
cout << q.front() << " ";
q.pop();
}
cout << endl;
}
int main()
{
test();
system("pause");
return 0;
}