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字 ...
随机推荐
- ES查询-match VS match_phrase
我们以一个查询的示例开始,我们在student这个type中存储了一些学生的基本信息,我们分别使用match和match_phrase进行查询. 首先,使用match进行检索,关键字是“He is”: ...
- 苹果手机input有圆角阴影的解决方法
input[type=button], input[type=submit], input[type=file], button { cursor: pointer; -webkit-appearan ...
- Python中安装bs4后,pycharm依然报错ModuleNotFoundError: No module named 'bs4'
学习网络抓取时,第一步出现问题. 执行示例代码 from urllib.request import urlopen from bs4 import BeautifulSoup html = urlo ...
- FastDFS api介绍
1. 命令行api介绍 FastDFS提供了可用于运维测试的命令行api,下面进行介绍: 1.1 fastdfs服务管理 tracker进程服务管理脚本 /etc/init.d/fdfs_tracke ...
- c++常用小算法
这篇文章列出了一些简单常用的C++容器算法,C++标准库中事实上提供了很多的算法并且有详细的介绍.如果需要详细的了解这些算法可以 参考C++在线参考手册 algrithm . 1 排序 在 #incl ...
- RIDE安装操作
转载参考https://www.cnblogs.com/Ming8006/p/4998492.html 一.python安装 1.访问Python官网:https://www.python.org/ ...
- Non-decreasing Array LT665
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- SystemUI中设置横竖屏显示
SystemUI中快捷菜单有 “方向锁定” . RotationLockTile protected void handleClick() { if (mController == null) ret ...
- nginx常用服务配置
一.nginx.conf的配置方式,创建新vhost user nginx; worker_processes ; worker_cpu_affinity ; worker_rlimit_nofile ...
- 解决mysql for docker容器报错:Authentication plugin 'caching_sha2_password' cannot be loaded
为图方便,懒得在mac上安装mysql了,一个是管理不方便,第二个是为了方便多机器同步开发环境.就使用docker安装了. 拉取mysql镜像 docker pull mysql 运行mysql实例 ...