STL - queue(队列)
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
}
demo
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
void queueInit()
{
queue<int> q;
q.push(1);
q.push(3);
q.push(5);
cout << "size of q: " << q.size() << endl;
// size of q: 3
cout << "front element: " << q.front() << endl;
// front element: 1
while (!q.empty()) {
cout << q.front() << ' ';
q.pop();
}
// 1 3 5
cout << endl;
}
class Teacher
{
public:
int age;
char name[32];
public:
void printTeacher()
{
cout << "age: " << age << endl;
}
};
void queueClass()
{
Teacher t1, t2, t3;
t1.age = 21;
t2.age = 22;
t3.age = 23;
queue<Teacher> q1;
q1.push(t1);
q1.push(t2);
q1.push(t3);
while (!q1.empty()) {
Teacher tmp = q1.front();
q1.pop();
tmp.printTeacher();
}
cout << endl;
/*
age: 21
age: 22
age: 23
*/
queue<Teacher *> q2;
q2.push(&t1);
q2.push(&t2);
q2.push(&t3);
while (!q2.empty()) {
Teacher *tmp = q2.front();
q2.pop();
tmp->printTeacher();
}
cout << endl;
/*
age: 21
age: 22
age: 23
*/
}
int main()
{
queueInit();
queueClass();
return 0;
}
STL - queue(队列)的更多相关文章
- [STL] queue 队列 priority_queue 优先队列
- STL中队列(queue)的使用方法
STL 中队列的使用(queue) 基本操作: push(x) 将x压入队列的末端 pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值 front() 返回第一个元素(队顶元素) ...
- STL Queue 容器
STL Queue 容器 Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. # ...
- 浅谈C++ STL queue 容器
浅谈C++ STL queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(queue\)容器的使用方法和常见的使用技巧.\(queue\)容器是\(C++STL\)的一种比较基本的容器.我们 ...
- C++ STL - queue常见函数使用解析
C++ STL - queue常见函数使用解析 c++队列模板类的定义在头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque ...
- C#基础---Queue(队列)的应用
Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...
- 第19章 queue队列容器
/* 第19章 queue队列容器 19.1 queue技术原理 19.2 queue应用基础 19.3 本章小结 */ // 第19章 queue队列容器 // 19.1 queue技术原理 // ...
- atitit. java queue 队列体系and自定义基于数据库的队列总结o7t
atitit. java queue 队列体系and自定义基于数据库的队列总结o7t 1. 阻塞队列和非阻塞队列 1 2. java.util.Queue接口, 1 3. ConcurrentLink ...
- C#部分---特殊集合:stack栈集合、queue队列集合、哈希表集合。
1.stack栈集合:又名 干草堆集合 栈集合 特点:(1)一个一个赋值 一个一个取值(2)先进后出实例化 初始化 Stack st = new Stack(); //添加元素用push st.Pus ...
- 实现一个线程安全的Queue队列
使用装饰者模式实现一个线程安全的Queue队列. public class SynchronizedQueue<E> implements Queue<E>, Serializ ...
随机推荐
- mysql 常见语句
事务 默认是开启了autocommit,可以通过show variables like 'autocommit';查看. 如果临时需要取消autocommit,可以通过START TRANSACTIO ...
- UDP单播和组播使用SO_REUSEADDR 测试结果
UDP单播通信 一. 预置条件 A.B在同一台机器,网络中存在往A.B所在的机器的8888端口发送单播UDP数据 A:端口复用绑定在端口8888上 B:端口复用绑定在端口8888上操作步骤:(1)先启 ...
- Mac 下 查看 使用某端口的进程和关闭该进程的命令
查看使用某端口的进程 最简单的命令是: lsof -i :端口号 如果要使用管理员权限那么就是: sudo lsof -i :端口号 所以查看 使用某端口号3000的进程可以使用: lsof -i : ...
- PGM:贝叶斯网的参数估计2
http://blog.csdn.net/pipisorry/article/details/52599321 没时间看了,下次再看... 具有共享参数的学习模型 全局参数共享 局部参数共享 具有 共 ...
- activiti 数据库连接配置
1.1.1. 前言 在activiti 动态配置 activiti 监听引擎启动和初始化(高级源码篇)一文中,我们讲解了如何动态的配置DataSource 当我们程序配置了DataSource,act ...
- IE下的deflate模式
浏览器有一个非常有用的特性:自动解压. 在使用AJAX请求数据的时候,数据在服务器端压缩传输,在浏览器端自动解压,请求直接得到解压后的结果. 在Request Header中,一般会列出浏览器支持的压 ...
- Android数据库Sqlite-android学习之旅(九)
简介 sqilte是一个轻量级的数据库,满足数据库的基本操作,由于移动端的内存有限,所以sqilte刚好能满足移动端开发的基本要求. 废话不多说,上代码 1.首先介绍一下,sqlite的管理类SQLi ...
- 人类创造未来的思想先锋:这些 TED 演示深深震撼着我们
今年亮点之一是谷歌创始人拉里佩奇的演讲.他有一个核心观点:特别成功的公司,是那些敢于想象未来,并付出行动创造未来的公司.这听上去是老生常谈,但又确实是个真理.他实际上想说预测未来的最好方式就是创造它, ...
- xml解析之sax解析原理图和技术介绍
SAX解析工具- Sun公司提供的.内置在jdk中.org.xml.sax.* 核心的API: SAXParser类: 用于读取和解析xml文件对象 parse(File f,DefaultHand ...
- Android 6.0 运行时权限处理问题
序 自从升级到Android M以来,最大的改变就是增加了运行时权限RuntimePermission,6.0以上的系统如果没有做适配,运行了targetSDK=23的App时就会报权限错误.我们知道 ...