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. HDU 5742 It's All In The Mind (贪心)

    It's All In The Mind 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5742 Description Professor Zhan ...

  2. [iOS UI进阶 - 1] 自定义控件

    A.关于Quiartz2D的一些细节 1.UIKit的工具已经封装了上下文引用,所以不用手动获取和渲染 - (void)drawRect:(CGRect)rect { [[UIColor redCol ...

  3. Windows Server2008 R2 MVC 环境配置

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  4. How Much Work Does it Take to be a Successful Mathematician?

    http://mathoverflow.net/questions/9799/how-much-work-does-it-take-to-be-a-successful-mathematician# ...

  5. IOS Note - Outlet(插座) & Action(动作)

    OutletActionViewController.h #import <UIKit/UIKit.h> @interface OutletActionViewController : U ...

  6. uoj #139. 【UER #4】被删除的黑白树 dfs序 贪心

    #139. [UER #4]被删除的黑白树 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/139 Descript ...

  7. Java内部类的自我理解

    本文借鉴网络上多位大牛的博客和文章.感谢各位不知名人士的分享. 一.什么事内部类? 内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的成员,而且依附于外部类而存在的.内部类能够为静态,可用p ...

  8. android151 笔记

    13. 14 .什么是Service以及描述下它的生命周期.Service有哪些启动方法,有什么区别,怎样停用Service? 在Service的生命周期中,被回调的方法比Activity少一些,只有 ...

  9. MySQL join buffer使用

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://huanghualiang.blog.51cto.com/6782683/12 ...

  10. TortoiseGit安装教程

    TortoiseGit 是Windows下的可视化Git界面. 下载Git 网站地址: http://code.google.com/p/tortoisegit/ 安装前必须装上msysgit才能在W ...