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的更多相关文章

  1. C++STL学习笔记_(1)string知识

    /*============================================ string是STL的字符串类型,通常用来表示字符串 = ======================== ...

  2. C++STL学习笔记_(1)deque双端数组知识

    #include<iostream> using namespace std; #include "deque" #include "algorithm&qu ...

  3. C++STL学习笔记_(1)vector知识

    #include<iostream> using namespace std; #include "vector" //数组元素的 添加和删除 void main31( ...

  4. C++STL学习笔记_(3)stack

    10.2.4stack容器 Stack简介 ²  stack是堆栈容器,是一种"先进后出"的容器. ²  stack是简单地装饰deque容器而成为另外的一种容器. ²  #inc ...

  5. C++STL学习笔记_(2)deque双端数组知识

    #include<iostream> using namespace std; #include "deque" #include "algorithm&qu ...

  6. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  7. 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 ...

  8. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  9. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

随机推荐

  1. [iOS基础控件 - 6.11.5] 沙盒 & 数据存储

    A.沙盒 每个APP都有一个沙盒,是独立存在的   1.Xcode5和Xcode6的模拟器文件目录 a.模拟器路径改版 (1)Xcode5中模拟器路径为:/Users/用户名/Library/Appl ...

  2. Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)

    题目链接:http://codeforces.com/problemset/problem/697/D 给你一个有规则的二叉树,大概有1e18个点. 有两种操作:1操作是将u到v上的路径加上w,2操作 ...

  3. FloatingActionButton的一点学习感悟

    最近在学习android材料设计的新控件,前面一篇文章讲到 CoordinatorLayout 结合几个新控件可以实现的几个效果.其中第一个是,Coordinatorlayout + Floating ...

  4. javascript:history.go(-1);

    history是你浏览过的网页的url(简单的说就是网址)的集合,也就是你的浏览器里的那个历史记录.它在js里是一个内置对象,就跟document一样,它有自己的方法,go就是其中一个. 这个方法的参 ...

  5. C# 解压zip压缩文件

    此方法需要在程序内引用ICSharpCode.SharpZipLib.dll 类库 /// <summary> /// 功能:解压zip格式的文件. /// </summary> ...

  6. CCLabelTTF、CCLabelAtlas和CCLabelBMFont的区别

    转自:http://blog.sina.com.cn/s/blog_67a5e47201018tj8.html 在Cocos2d以及Cocos2d-x中,我们经常会用到CCLabelTTF以及CCLa ...

  7. Codeforces Gym H. Hell on the Markets 贪心

    Problem H. Hell on the MarketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vj ...

  8. 用VS2010开发Android应用的配置方法

    在开发你的第一个Android应用程序之前,你应该先检查一下是否安装了Android SDK,以及是否创建好了Android模拟器(AVD),如果有不清楚的地方,请先看百度这篇文章“Android是什 ...

  9. iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)

    前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...

  10. 免费的天气预报API--谷歌,雅虎,中央气象台

    Google Weather API 仅仅支持美国地区使用邮政编码进行查询,比如:  http://www.google.com/ig/api?hl=zh-cn&weather=94043  ...