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. CodeForces 176B Word Cut (计数DP)

    Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  2. 删除MySQL重复数据

    删除MySQL重复数据 项目背景 在最近做的一个linux性能采集项目中,发现线程的程序入库很慢,再仔细定位,发现数据库里面很多冗余数据.因为在采集中,对于同一台设备,同一个时间点应该只有一个数据,然 ...

  3. CodeForces 707B Bakery (水题,暴力,贪心)

    题意:给定n个城市,其中有k个有仓库,问你在其他n-k个城市离仓库的最短距离是多少. 析:很容易想到暴力,并且要想最短,那么肯定是某一个仓库和某一个城市直接相连,这才是最优,所以只要枚举仓库,找第一个 ...

  4. Python 3.2: 使用pymysql连接Mysql

    在python 3.2 中连接MYSQL的方式有很多种,例如使用mysqldb,pymysql.本文主要介绍使用Pymysql连接MYSQL的步骤 1        安装pymysql ·       ...

  5. OpenNebula config配置详情

    AUTH_MAD=AUTHN=ssh,x509,ldap,server_cipher,server_x509,EXECUTABLE=one_auth_mad DATASTORE_BASE_PATH=/ ...

  6. MyEclipse10.7的 at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>

      前两天由于换了MyEclipse新版本之后,我的MyEclipse的Servers就不能正常使用了,也就是不能发布Web项目了.出现了空指针的异常,并产生了这个错误: atcom.genuitec ...

  7. Python3.5.2官方文档学习备忘录

    网址:https://docs.python.org/3/ 虽然学习官方文档有些耗时,不过看最原版的还是感觉好一点,原汁原味没有曲解没有省略. 从命令行向Python传递参数,运行:python - ...

  8. java 数据库连接池 Oracle版

    首先应加入连接池和数据库连接的配置文件:数据库连接包:ojdbc6.jar数据库连接池包:commons-pool2-2.2.jar                       commons-dbc ...

  9. WCF 新手教程二

    基本知识: [ServiceContract] Attribute 能够有以下Property 的: CallbackContract 设置callback的类型:Duplicate指Service ...

  10. C语言用static限制函数以及全局变量的作用域

    今天才发现这个东西! C语言中没有public private之类的东西. 如果一个函数或者一个全局变量只想在一个.c文件中使用,可以在前面加上static! 以前我还傻傻的每个.c文件中的函数都加一 ...