Queue容器
来源:互联网 发布:mac itunes 铃声找不到 编辑:程序博客网 时间:2024/05/22 04:40
1、Queue简介
queue是队列容器,是一种“先进先出”的容器。
queue是简单地装饰deque容器而成为另外的一种容器。
#include <queue>
2、queue对象的默认构造
queue采用模板类实现,queue对象的默认构造形式:queue<T>queT; 如:
queue<int> queInt; //一个存放int的queue容器。
queue<float> queFloat; //一个存放float的queue容器。
queue<string> queString; //一个存放string的queue容器。
//尖括号内还可以设置指针类型或自定义类型。
3、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
4、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; //赋值
5、queue的数据存取
queue.back(); //返回最后一个元素
queue.front(); //返回第一个元素
queue<int>queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
intiFront = queIntA.front(); //1
intiBack = queIntA.back(); //9
queIntA.front()= 11; //11
queIntA.back()= 19; //19
6、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())
{
intiSize = queIntA.size(); //5
}
7、示例代码:
#include<iostream>using namespace std;#include<queue>// 队列中基本数据类型void test(){queue<int> q;q.push(1);q.push(2);q.push(3);cout << "对头元素:"<<q.front() << endl;cout << "对尾元素:" << q.back() << endl;while (!q.empty()){int tmp = q.front();cout << tmp << " ";q.pop();}cout << endl;}class Teacher{public:void printT(){cout << "age: " << age << endl;}public:int age;char name[10];};// 队列中的算法 和 数据类型的分离void test1(){Teacher t1, t2, t3;t1.age = 22;t2.age = 23;t3.age = 24;queue<Teacher> q;q.push(t1);q.push(t2);q.push(t3);while (!q.empty()){Teacher tmp = q.front();tmp.printT();q.pop();}}void test2(){Teacher t1, t2, t3;t1.age = 22;t2.age = 23;t3.age = 24;queue<Teacher*> q;q.push(&t1);q.push(&t2);q.push(&t3);while (!q.empty()){Teacher *tmp = q.front();tmp->printT();q.pop();}}int main(void){// test();// test1();test2();return 0;}
- queue容器
- queue容器
- queue 容器
- Queue容器
- STL容器适配器:queue
- c++queue容器介绍
- Queue队列容器
- 容器适配器之queue
- java Queue容器实现
- C++ queue容器
- Queue队列容器
- c++queue容器介绍
- C++容器之Queue
- queue容器适配器
- java Queue容器实现
- c++queue容器介绍
- queue队列容器
- C++之容器queue
- Gartner公布2018十大技术发展趋势 2025年AI重塑商业模式
- [Node.js]Linux 快速安装Node.js
- UE4-C++核心编程之接口:接口在蓝图中的使用
- Spring+SpringMVC+Hibernate
- 《Fluentd 入门》笔记
- Queue容器
- java应届毕业生面试技巧
- C语言开发小练习3
- JS-RGB转16进制(rgb2hex)
- 定时任务调度之Timer---综合使用小案例
- 虚拟化与Docker
- rabbitmq基础
- TensorFlow 记录
- 97. Interleaving String