C++STL学习笔记_(4)queue
10.2.5Queue容器
Queue简介
² queue是队列容器,是一种“先进先出”的容器。
² queue是简单地装饰deque容器而成为另外的一种容器。
² #include <queue>
queue对象的默认构造
queue采用模板类实现,queue对象的默认构造形式:queue<T> queT; 如:
queue<int> queInt; //一个存放int的queue容器。
queue<float> queFloat; //一个存放float的queue容器。
queue<string> queString; //一个存放string的queue容器。
...
//尖括号内还可以设置指针类型或自定义类型。
queue的push()与pop()方法
queue.push(elem); //往队尾添加元素
queue.pop(); //从队头移除第一个元素
queue<int> queInt;
queInt.push(1);queInt.push(3);
queInt.push(5);queInt.push(7);
queInt.push(9);queInt.pop();
queInt.pop();
此时queInt存放的元素是5,7,9
queue对象的拷贝构造与赋值
queue(const queue &que); //拷贝构造函数
queue& operator=(const queue &que); //重载等号操作符
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
queue<int> queIntB(queIntA); //拷贝构造
queue<int> queIntC;
queIntC = queIntA; //赋值
queue的数据存取
² queue.back(); //返回最后一个元素
² queue.front(); //返回第一个元素
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
int iFront = queIntA.front(); //1
int iBack = queIntA.back(); //9
queIntA.front() = 11; //11
queIntA.back() = 19; //19
queue的大小
² queue.empty(); //判断队列是否为空
² queue.size(); //返回队列的大小
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
if (!queIntA.empty())
{
int iSize = queIntA.size(); //5
}
#include<iostream>
using namespace std; #include<queue> void main61()
{
queue<int> q;
q.push();
q.push();
q.push(); cout<<"获取队头元素"<<q.front()<<endl;
cout<<"队列的大小"<<q.size()<<endl; while (!q.empty())
{
int tmp = q.front();
cout<<q.front()<<" ";
q.pop();
}
} //队列算法和数据类型的分离 //teacher节点
class Teacher
{
public:
int age;
char name[];
public:
void prinT()
{
cout<<"age:"<<age<<endl;
}
}; void main62()
{
Teacher t1,t2,t3;
t1.age = ;
t2.age = ;
t3.age = ;
queue<Teacher> q;
q.push(t1);
q.push(t2);
q.push(t3); while (!q.empty())
{
Teacher tmp = q.front();
tmp.prinT();
q.pop();
} } void main63()
{
Teacher t1,t2,t3;
t1.age = ;
t2.age = ;
t3.age = ;
queue<Teacher*> q;
q.push(&t1);
q.push(&t2);
q.push(&t3); while (!q.empty())
{
Teacher *tmp = q.front();
tmp->prinT();
q.pop();
} } void main()
{
//main61();
main62();
main63();
cout<<"hello..."<<endl;
return;
}
资料来源:传智播客
最新文章
- MWeb 1.6 发布!Dark Mode、全文搜寻、发布到Wordpress、Evernote 等支持更新、编辑/预览视图模式等
- C语言:void指针
- [HDOJ5763]Another Meaning(KMP, DP)
- dom 回到顶部(兼容IE FF Chrome)
- java-base64
- 自学Zabbix1.3-zabbix进程
- hadoop解决Could not locate executable null\bin\winutils.exe in the Hadoop binaries.问题
- 【mmall】Jackson序列化时isSuccess()方法的注意点
- 011 pandas的常见操作
- Mongo 查询(可视化工具)
- 百度接口test
- Wrapper
- C#学习笔记(九):函数、代码查询和调试
- bzoj1257: [CQOI2007]余数之和 整除分块
- 深度扫盲JavaScript的模块化(AMD , CMD , CommonJs 和 ES6)
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
- 【zzuli-2276】跳一跳
- 【BZOJ】1680: [Usaco2005 Mar]Yogurt factory(贪心)
- MAVEN本地下载、安装
- Hibernate 简易入门教程