C++ 知识回顾总结 -- queue 队列容器
一、说明
queue 是一种队列适配器,专门设计用于FIFO中操作(先进先出),元素从一端插入容器并从另一端提取。
相关API地址为:http://www.cplusplus.com/reference/queue/queue/
二、使用方法
在C++中只要#include<queue>即可使用队列类,其中在面试或笔试中常用的成员函数如下(按照最常用到不常用的顺序)
push、pop、size、empty、front、back
接下来逐一举例说明:
1. push
队列中由于是先进先出,push即在队尾插入一个元素,如:
1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 cout<<q.front()<<endl;
可以输出:Hello World!
2. pop
将队列中最靠前位置的元素拿掉,是没有返回值的void函数。如:
1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 q.pop();
5 cout<<q.front()<<endl;
可以输出:China
原因是Hello World!已经被除掉了。
3. size
返回队列中元素的个数,返回值类型为unsigned int。如:
queue<string> q;
cout<<q.size()<<endl;
q.push("Hello World!");
q.push("China");
cout<<q.size()<<endl;
输出两行,分别为0和2,即队列中元素的个数。
4. empty
判断队列是否为空的,如果为空则返回true。如:
1 queue<string> q;
2 cout<<q.empty()<<endl;
3 q.push("Hello World!");
4 q.push("China");
5 cout<<q.empty()<<endl;
输出为两行,分别是1和0。因为一开始队列是空的,后来插入了两个元素。
5. front
返回值为队列中的第一个元素,也就是最早、最先进入队列的元素。注意这里只是返回最早进入的元素,并没有把它剔除出队列。如:

1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 cout<<q.front()<<endl;
5 q.pop();
6 cout<<q.front()<<endl;

输出值为两行,分别是Hello World!和China。只有在使用了pop以后,队列中的最早进入元素才会被剔除。
6. back
返回队列中最后一个元素,也就是最晚进去的元素。如:
1 queue<string> q;
2 q.push("Hello World!");
3 q.push("China");
4 cout<<q.back()<<endl;
输出值为China,因为它是最后进去的。这里back仅仅是返回最后一个元素,也并没有将该元素从队列剔除掉。
三、使用链表将queue实现
#include<iostream>
#include<string>
using namespace std; template <typename T>
struct Node{
T value;
Node<T> *next;
Node<T>(){next = NULL;}
}; template <typename T>
class MyQueue{
private:
unsigned int num;
Node<T> *first;
Node<T> *last; public:
MyQueue();
~MyQueue();
unsigned int size();
void push(T element);
void pop();
bool empty();
T back();
T front();
}; template <typename T>
MyQueue<T>::MyQueue(){
num = ;
first = NULL;
last = NULL;
} template <typename T>
MyQueue<T>::~MyQueue(){
while(!empty()){
pop();
}
} template <typename T>
unsigned int MyQueue<T>::size(){
return num;
} template <typename T>
bool MyQueue<T>::empty(){
return (==num);
} template <typename T>
void MyQueue<T>::push(T element){
Node<T> *temp = new Node<T>;
temp->next = NULL;
temp->value = element;
if ( == this->num){
first = temp;
last = temp;
}else{
last->next = temp;
last = temp;
}
(this->num)++;
} template <typename T>
void MyQueue<T>::pop(){
if (==this->num){
cout<<"No elements in the queue!"<<endl;
}else if( == this->num){
delete first;
first = NULL;
last = NULL;
this->num = ;
}else{
Node<T> *temp = first;
first = first->next;
delete temp;
(this->num)--;
}
} template <typename T>
T MyQueue<T>::back(){
if (==this->num){
cout<<"No elements in the queue!"<<endl;
return NULL;
}
return last->value;
} template <typename T>
T MyQueue<T>::front(){
if(== this->num){
cout<<"No elements in the queue!"<<endl;
return NULL;
}
return first->value;
} int main(){
MyQueue<string> q;
q.push("Hello world");
q.push("China");
cout<<q.front()<<endl;
cout<<q.size()<<endl;
cout<<q.back()<<endl;
q.pop();
cout<<q.empty()<<endl;
cout<<q.back()<<endl;
cout<<q.front()<<endl;
q.pop();
cout<<q.size()<<endl;
cout<<q.empty()<<endl;
system("pause");
return ;
}
C++ 知识回顾总结 -- queue 队列容器的更多相关文章
- 第19章 queue队列容器
/* 第19章 queue队列容器 19.1 queue技术原理 19.2 queue应用基础 19.3 本章小结 */ // 第19章 queue队列容器 // 19.1 queue技术原理 // ...
- stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)
stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ...
- python2.0_s12_day9之day8遗留知识(queue队列&生产者消费者模型)
4.线程 1.语法 2.join 3.线程锁之Lock\Rlock\信号量 4.将线程变为守护进程 5.Event事件 * 6.queue队列 * 7.生产者消费者模型 4.6 queue队列 que ...
- Docker 核心知识回顾
Docker 核心知识回顾 最近公司为了提高项目治理能力.提升开发效率,将之前的CICD项目扩展成devops进行项目管理.开发人员需要对自己的负责的项目进行流水线的部署,包括写Dockerfile ...
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...
- Java基础知识回顾之七 ----- 总结篇
前言 在之前Java基础知识回顾中,我们回顾了基础数据类型.修饰符和String.三大特性.集合.多线程和IO.本篇文章则对之前学过的知识进行总结.除了简单的复习之外,还会增加一些相应的理解. 基础数 ...
- python---基础知识回顾(六)网络编程
python---基础知识回顾(十)进程和线程(进程) python---基础知识回顾(十)进程和线程(多线程) python---基础知识回顾(十)进程和线程(自定义线程池) 一:Socket (一 ...
- scrapy实战1,基础知识回顾和虚拟环境准备
视频地址 https://coding.imooc.com/learn/list/92.html 一. 基础知识回顾 1. 正则表达式 1)贪婪匹配,非贪婪匹配 .*? 非贪婪 . ...
- C#学习笔记(基础知识回顾)之值类型与引用类型转换(装箱和拆箱)
一:值类型和引用类型的含义参考前一篇文章 C#学习笔记(基础知识回顾)之值类型和引用类型 1.1,C#数据类型分为在栈上分配内存的值类型和在托管堆上分配内存的引用类型.如果int只不过是栈上的一个4字 ...
随机推荐
- http/2.0与http/1.1的区别
http/2是http协议自1999年http1.1发布后的首个更新 主要基于SPDY协议 2.0 采用二进制 而不是文本格式 完全多路复用 而不是有序并阻塞的 只需要一个连接即可实现并行 使 ...
- 大数据学习(二)-------- MapReduce
前提已经安装好hadoop的hdfs集群,可以查看 https://www.cnblogs.com/tree1123/p/10683570.html Mapreduce是hadoop的运算框架,可以对 ...
- CentOS7升级默认内核
安装内核升级镜像源 rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm Yum安装内核 yum --e ...
- 使用react-handsontable
新建一个项目 create-react-app myProject cd myProject npm install handsontable 或者 npm install handsontable- ...
- scrapy项目对接Docker
1.项目根目录生成requirements.txt文件 命令pip freeze > requirements.txt 2.项目根目录新建Dockerfile文件,文件不加任何后缀名 内容如下 ...
- Which Queue Pair type to use?
Which Queue Pair type to use? Contents [hide] 1 Reliable Connected (RC) QP 2 Unreliable Connected (U ...
- Linux vim常见使用详解
教你用Vim编辑器 1.Vim编辑器基本使用方法 光标移动 查找/替换 插入模式 复制/粘贴 复制/粘贴 2.vim打开时的警告信息 当使用vim打开一个文件时,会同时在该目录下创建个.filenam ...
- NodeManager介绍
原文链接: http://blog.csdn.net/zhangzhebjut/article/details/37730013 参考文档: https://blog.csdn.net/u013384 ...
- docker构建镜像
Docker 提供了两种构建镜像的方法: docker commit 命令Dockerfile 构建文件 示例: Dockerfile FROM golang:1.7.5 #基础镜像 RUN apt- ...
- reactjs 学习笔记
1.安装 npm install -g create-react-app create-react-app my-app cd my-app npm start