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 ...
随机推荐
- 在Spring Boot框架下使用WebSocket实现聊天功能
上一篇博客我们介绍了在Spring Boot框架下使用WebSocket实现消息推送,消息推送是一对多,服务器发消息发送给所有的浏览器,这次我们来看看如何使用WebSocket实现消息的一对一发送,模 ...
- Dubbo框架应用之(四)--Dubbo基于Zookeeper实现分布式实例
上三篇文章主要是解决了概念性的补充和学习,充分结合实战来深入理解 入门实例解析 第一:provider-提供服务和相应的接口 创建DemoService接口 package com.unj.dubbo ...
- Maven之(六)setting.xml配置文件详解
setting.xml配置文件 maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.h ...
- Spark:Spark 编程模型及快速入门
http://blog.csdn.net/pipisorry/article/details/52366356 Spark编程模型 SparkContext类和SparkConf类 代码中初始化 我们 ...
- Swift中switch强大的模式匹配
不少人觉得Swift中switch语句和C或C++,乃至ObjC中的差不多,此言大谬! 让本猫带领大家看一下Swift中switch语句模式匹配的威力. 所谓模式匹配就是利用一定模式(比如couple ...
- mvn管理项目jar包
Maven是一个采用纯Java编写的开 源项目管理工具.Maven采用了一种被称之为project object model (POM)概念来管理项目,所有的项目配置信息都被定义在一个叫做POM.xm ...
- Android获取当前网络状态
Android获取当前网络状态 效果图 有网络 没有网络 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9052 ...
- Spark技术内幕:Master的故障恢复
Spark技术内幕:Master基于ZooKeeper的High Availability(HA)源码实现 详细阐述了使用ZK实现的Master的HA,那么Master是如何快速故障恢复的呢? 处于 ...
- Linux系统编程----孤儿进程
什么是孤儿进程? 孤儿进程, 指在父进程退出后,而子进程还在运行,这个子进程就成了孤儿进程,这时由init进程(pid=1)接管 来看看例子: #include <stdio.h> #i ...
- Android开发学习之路--Activity之四种启动模式
后天终于可以回家了,马上就要过年了,趁着年底打酱油的模式,就多学习学习,然后记录记录吧.关于Activity已经学习了七七八八了,还有就是Activity的四种启动模式了,它们分别为,standard ...