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. 【HDOJ】5632 Rikka with Array

    1. 题目描述$A[i]$表示二级制表示的$i$的数字之和.求$1 \le i < j \le n$并且$A[i]>A[j]$的$(i,j)$的总对数. 2. 基本思路$n \le 10^ ...

  2. python学习,dict的映射练习

    练习dict的映射 #coding:utf-8 #问题: a->c, b->d, c->e... 现在有结果字符串求原字符串 dict1={'a':'c', 'b':'d', 'c' ...

  3. 1651. Shortest Subchain(bfs)

    1651 终于A了 看这题容易想到最短路 看到错的很多 还特意注意了好几处 后来发现 必须按给出的顺序出边 想了想 这不就是BFS 然后就是各种细节 i->i+1ori->j(a[i]== ...

  4. rapidxml使用

    以前都是用tinyxml,这次开发中解析xml配置文件像尝试一下rapidxml,据说效率很高... RapidXml Manual: http://rapidxml.sourceforge.net/ ...

  5. Fragment 和 FragmentActivity的使用(二)

      今天继续完成剩下的学习部分,现在项目很多地方使用viewpager来提供滑动,今天记录学习viewpager配合fragment的显示,增加一个CallLogsFragment配合之前SMSLis ...

  6. java 语言里 遍历 collection 的方式

    我来简单说一下吧,一般有2种方法来遍历collection中的元素,以HashSet为例子HashSet hs=new HashSet();hs.add("hello");hs.a ...

  7. bzoj1877: [SDOI2009]晨跑

    挺裸的最小费用最大流... #include<cstdio> #include<queue> #include<cstring> #include<iostr ...

  8. 通过CSS禁止Chrome自动为输入框添加橘黄色边框,修改/禁止 chrome input边框颜色,

    1   /*Chrome浏览器 点击input 黄色边框 禁用*/ .NoOutLine:focus{outline: none} <asp:TextBox ID="txtTeleph ...

  9. 海康、大华IpCamera RTSP地址和格式

    海康:rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream说明:username: 用户名.例如 ...

  10. 【剑指offer 面试题22】栈的压入、弹出序列

    思路: 不停地压栈,直到栈头元素与弹出序列的首元素相等则出栈,同时弹出序列后移:若不相等则一直保持压栈,直到压入所有元素后弹出序列仍不为空,则说明无法匹配. C++: #include <ios ...