1.队列queue特性
先进先出。
从一端(队尾)插入,另一端(队头)删除。
不提供迭代器,不能进行遍历,不支持随机访问。
2.队列queue构造函数
//默认构造函数
queue<T> queT;
//拷贝构造函数
queue(const queue &que);
3.队列queue存取、插入、删除操作
//向队尾添加元素
push(elem);
//从队头移除第一个元素
pop();
//返回最后一个元素
back();
//返回第一个元素
front();
4.队列queue赋值操作
//重载等号运算符
queue& operator(const queue &que);
5.队列queue大小判断
//判断队列是否为空
empty();
//返回队列的大小
size();
6.队列queue举例
#include <iostream>
#include <queue> //queue需要包含的头文件
using namespace std;
int main()
{
//初始化
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
cout<<"队尾:"<<q.back()<<endl;
while(q.size() > 0)
{
cout<<q.front()<<" ";
//删除队头元素
q.pop();
}
cout<<endl;
return 0;
}
执行结果:
root@host:/home/LinuxShare/007.STL# g++ 02queue.cpp
root@host:/home/LinuxShare/007.STL# ./a.out
队尾:40
10 20 30 40