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;
}
资料来源:传智播客
C++STL学习笔记_(4)queue的更多相关文章
- C++STL学习笔记_(1)string知识
/*============================================ string是STL的字符串类型,通常用来表示字符串 = ======================== ...
- C++STL学习笔记_(1)deque双端数组知识
#include<iostream> using namespace std; #include "deque" #include "algorithm&qu ...
- C++STL学习笔记_(1)vector知识
#include<iostream> using namespace std; #include "vector" //数组元素的 添加和删除 void main31( ...
- C++STL学习笔记_(3)stack
10.2.4stack容器 Stack简介 ² stack是堆栈容器,是一种"先进后出"的容器. ² stack是简单地装饰deque容器而成为另外的一种容器. ² #inc ...
- C++STL学习笔记_(2)deque双端数组知识
#include<iostream> using namespace std; #include "deque" #include "algorithm&qu ...
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
随机推荐
- Java虚拟机学习 - 体系结构 内存模型
一:Java技术体系模块图 二:JVM内存区域模型 1.方法区 也称"永久代” .“非堆”, 它用于存储虚拟机加载的类信息.常量.静态变量.是各个线程共享的内存区域.默认最小值为16MB,最 ...
- iOS异常捕获
文章目录 一. 系统Crash 二. 处理signal 下面是一些信号说明 关键点注意 三. 实战 四. Crash Callstack分析 – 进⼀一步分析 五. demo地址 六. 参考文献 前言 ...
- WinAPI: FindWindow、FindWindowEx - 查找窗口
FindWindow( lpClassName, {窗口的类名} lpWindowName: PChar {窗口的标题} ): HWND; {返回窗口的 ...
- poj3259
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24864 Accepted: 8869 Descri ...
- Linux命令行访问网页
找到个好资料,备份行: http://hi.baidu.com/oyvfhp/blog/item/3aa5ced5b40563d351da4bb0.html CURL --- 命令行浏览器 这东西 ...
- JS Math 类库介绍
下面介绍下随机生成数的常用几个API JS 随机数生成 : 在JavaScript , 提供了生成随机数的API, Math.random() 1.Math.random() : 随机生成小数 . 生 ...
- CSS3教程:Transform的perspective属性设置
1 2 <div id="animateTest" style="-webkit-transform: perspective(400px) rotateY(4 ...
- 基于RealSense的坐姿检测技术
计算机的飞速普及,让人们将越来越多的工作放在计算机上去完成,各行各业,尤其是程序开发人员.文字工作者,在计算机上的工作时间越来越长,这种情况下不良的坐姿对颈肩腰椎都会产生很大影响,容易导致多种疾病的发 ...
- cocos2d-x 仿真树叶飘落效果的实现
转自:http://blog.csdn.net/ufolr/article/details/7624851 最近项目中需要一个落叶的效果,本来想用粒子特效来实现,但是几经调试,虽然调出了落叶的效果,但 ...
- 【转】【React Native开发】
[React Native开发]React Native控件之ListView组件讲解以及最齐全实例(19) [React Native开发]React Native控件之Touchable*系列组 ...