使用C++中queue容器时必须包含
#include <queue>
using namespace std;
queue基本概念
queue是一种先进先出的数据结构,有两个出口,一端新增元素,另一端移除元素;
队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为
queue常用接口
①queue<T> q;
采用模板类实现,默认构造函数
②queue(const queue &q);
拷贝构造函数
③queue& operator=(const queue &q);
重载=操作符
④push(elem);
往队尾添加元素
⑤pop();
从队头移除第一个元素
⑥back();
返回最后一个元素
⑦front();
返回第一个元素
⑧empty();
判断队列是否为空
⑨size();
返回队列的大小
void test() {
queue<int> q; //默认构造函数
q.push(1); //向队尾添加元素1
q.push(2);
q.push(3);
q.push(4);
q.push(5);
queue<int> q1(q); //拷贝构造函数