Queue简介

  • queue是队列容器,是一种“先进先出”的容器。
  • queue是简单地装饰deque容器而成为另外的一种容器。
  • #include <queue>

1.queue对象的默认构造

queue采用模板类实现,queue对象的默认构造形式:queue<T> queT;  如:
queue<int> queInt; //一个存放int的queue容器。
queue<float> queFloat; //一个存放float的queue容器。
...
//尖括号内还可以设置指针类型或自定义类型。

2.queue的push()与pop()方法

  • queue.push(elem);   //往队尾添加元素
  • queue.pop();   //从队头移除第一个元素
#include<iostream>
using namespace std;
#include <queue>
void objPlay2()
{
queue<int> queInt;
queInt.push();
queInt.push();
queInt.push();
queInt.push();
queInt.push();
queInt.pop();
queInt.pop();//此时queInt存放的元素是5, 7, 9 }
int main()
{
objPlay2();
return ;
}

3.queue对象的拷贝构造与赋值

  • queue(const queue &que);                    //拷贝构造函数
  • queue& operator=(const queue &que); //重载等号操作符
void objPlay3()
{
queue<int> queIntA;
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push(); queue<int> queIntB(queIntA); //拷贝构造
queue<int> queIntC;
queIntC = queIntA; //赋值 }

4.queue的数据存取

  • queue.back();   //返回最后一个元素
  • queue.front();   //返回第一个元素
void objPlay4()
{
queue<int> queIntA;
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push(); int iFront = queIntA.front(); //获取队列的头元素,1
int iBack = queIntA.back(); //获取队列的尾元素 ,9 queIntA.front() = ; //
queIntA.back() = ; // }

5.queue的大小

  • queue.empty();   //判断队列是否为空
  • queue.size();          //返回队列的大小
    void objPlay5()
    {
    queue<int> queIntA;
    queIntA.push();
    queIntA.push();
    queIntA.push();
    queIntA.push();
    queIntA.push(); if (!queIntA.empty())
    {
    int iSize = queIntA.size(); //队列中有五个元素
    } }

 以上所有代码整理:

#include<iostream>
using namespace std;
#include <queue>
void objPlay2()
{
queue<int> queInt;
queInt.push();
queInt.push();
queInt.push();
queInt.push();
queInt.push();
queInt.pop();
queInt.pop();//此时queInt存放的元素是5, 7, 9 }
void objPlay3()
{
queue<int> queIntA;
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push(); queue<int> queIntB(queIntA); //拷贝构造
queue<int> queIntC;
queIntC = queIntA; //赋值 }
void objPlay4()
{
queue<int> queIntA;
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push(); int iFront = queIntA.front(); //获取队列的头元素,1
int iBack = queIntA.back(); //获取队列的尾元素 ,9 queIntA.front() = ; //
queIntA.back() = ; // }
void objPlay5()
{
queue<int> queIntA;
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push();
queIntA.push(); if (!queIntA.empty())
{
int iSize = queIntA.size(); //队列中有五个元素
} } int main()
{
objPlay2();
objPlay3();
objPlay4();
objPlay5(); return ;
}

STL学习系列五:Queue容器的更多相关文章

  1. 标准模板库(STL)学习探究之vector容器

    标准模板库(STL)学习探究之vector容器  C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...

  2. scrapy爬虫学习系列五:图片的抓取和下载

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  3. STL学习系列之一——标准模板库STL介绍

    库是一系列程序组件的集合,他们可以在不同的程序中重复使用.C++语言按照传统的习惯,提供了由各种各样的函数组成的库,用于完成诸如输入/输出.数学计算等功能. 1. STL介绍 标准模板库STL是当今每 ...

  4. 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)

    layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ...

  5. STL学习系列七:优先级队列priority_queue容器

    1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...

  6. STL学习系列三:Deque容器

    1.Deque简介 deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. deque在接口上和vector非常 ...

  7. STL学习系列二:Vector容器

    1.Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添 ...

  8. STL学习系列四:Stack容器

    Stack简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. #include <stack> 1.stack对象的默认构造 ...

  9. STL学习系列九:Map和multimap容器

    1.map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...

随机推荐

  1. Java视频教程

    http://outofmemory.cn/java/video/ http://outofmemory.cn/tutorial/

  2. C# WebBrowser准确判断网页最终装载完毕

    == 最近写了个软件叫WebAutoScript,目的用于,网页的自动操作处理,就是说,所有你在网页上面的操作,都可以录到一个脚本中,然后可以回放这个操作过程..我是说任何过程. 程序是用C#写的,其 ...

  3. 双方都在线,qq总是离线发文件

    这是qq支持多地登录后出现的问题. 原因:1.当您传文件给对方,对方是多终端登录(或者开通移动在线功能)的情况下,为了保证对方一定能收到该文件,我们会智能的为用户切换到离线文件,对方会相应在所在的终端 ...

  4. HDU 4893 线段树

    比赛时太大意,斐波拉契数列开小了. 题目大意:1个序列,3种操作,改变序列某个数大小,将序列中连续的一段每个数都变成其最近的斐波拉契数,以及查询序列中某一段的数之和. 解题思路:维护add[]数组表示 ...

  5. 函数 page_dir_get_n_heap

    查看某page中含有的记录个数 #define PAGE_N_HEAP 4 /* number of records in the heap, bit =flag: new-style compact ...

  6. 利用序列化的方式实现C#深复制和浅复制

    代码如下:具体看注释 [Serializable] public class A { public virtual string Name { get; set; } public int Age { ...

  7. Android基础_1 四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service(服务),Content Provider(内容提供者),BroadcastReceiver(广播接收器). 一.四大基本组件 Acti ...

  8. https实现安全传输的流程

    HTTPS简介 HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块.服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后 ...

  9. windows批处理中的%0 %1 %2 %3

    原来就是参数的顺序.....倒...我还查了老半天

  10. “FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”。如果是有意隐藏,请使用关键字 new。

    一旦运行就显示:“FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”.如果是有意隐藏,请使用关键字 ne ...