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 ...
随机推荐
- 如何扩展/删除swap分区
背景: 由于安装Oracle 的时候,swap太小只划分了4G,后期发现交换分区太小,不满足使用,于是进行了swap分区的扩容过程: swap分区的扩展很简单,但是需要root用户权限 ...
- linux下的环境变量
环境变量有时候要查找,但是经常忘记有哪些文件,现在做一个总结: /etc/profile 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/e ...
- 携程React Native实践
React Native(下文简称 RN)开源已经一年多时间,国内各大互联网公司都在使用,携程也在今年 5 月份投入资源开始引入,并推广给多个业务团队使用,本文将会分享我们遇到的一些问题以及我们的优化 ...
- 创建银行分行的API
DECLARE p_api_version NUMBER := 1.0; p_init_msg_list VARCHAR2(1) := 'F'; v_bank_id NUMBER := 530705; ...
- 【ShaderToy】开篇
写在前面 呜呼,好久没有写博客了,好惭愧.题外话,感觉越大就越想家,希望可以一直和家人在一起,哪怕只是坐在一起不说话也觉得很温暖,一想到要分开眼睛就开始酸,哎.开学还是爬上来老实更新博客学习吧~ 今天 ...
- 【Unity技巧】四元数(Quaternion)和旋转
四元数介绍 旋转,应该是三种坐标变换--缩放.旋转和平移,中最复杂的一种了.大家应该都听过,有一种旋转的表示方法叫四元数.按照我们的习惯,我们更加熟悉的是另外两种旋转的表示方法--矩阵旋转和欧拉旋转. ...
- 关于V4L2中操作比较重要的几个命令以及一般操作流程总结
最近在做关于摄像头测试程序相关的一些开发,主要是想要实现在摄像头采集视频的过程中,通过按键来实现拍照,然后将拍照得到的数据保存到一个文件中,通过了解V4L2的一些相关操作,原来,拍照后的数据是保存在一 ...
- 简单RPC实现之Netty实现
所谓RPC就是远程方法调用(Remote Process Call ),简单的来说就是通过MQ,TCP,HTTP或者自己写的网络协议来传输我要调用对方的什么接口,对方处理之后再把结果返回给我.就这么 ...
- 最简单的基于Flash的流媒体示例:RTMP推送和接收(ActionScript)
===================================================== Flash流媒体文章列表: 最简单的基于Flash的流媒体示例:RTMP推送和接收(Acti ...
- Ubuntu 15.10下Qt5的安装实战
写照篇博客的目的就是因为最近要使用Qt,但是由于本人的系统是Ubuntu的,而网上大部分的讲解全是基于Windows的,所以就花费一些时间总结了一下我的安装过程,当然也是也为了能帮助到更多的博友. 第 ...