当前位置:网站首页>【STL】容器适配器之stack、queue用法总结
【STL】容器适配器之stack、queue用法总结
2022-06-23 05:57:00 【舒泱】
一、基本原理
除了顺序容器外,标准库还提供了三种容器适配器(container adapter):栈stack、队列queue、优先队列priority_queue。stack和queue基于deque实现,priority_queue基于vector实现。限于篇幅,本篇博客只介绍stack和queue。
什么是容器适配器呢?
假设我需要一个栈结构,可以用deque来模拟,只在一端进行元素插入和弹出,另一端不动,但我不能防止别人在deque的另一端进行操作,因此deque并不能严格地满足我的要求。我对它进行封装,作一些限制,使它留出的接口只能在一端进行插入和删除,这样就实现了stack。
实际上stack底层是使用的deque,只是对deque进行了封装改变了对外的接口而已。因此,stack、queue、priority_queue一般称为容器适配器,它们只是基本容器类型(vector、deque、list等)的适配。
二、用法
以下都以容器中的元素为int为例,若要保存其他类型的元素,将int换成其他类型即可。
初始化
| stack | stack<类型名> 栈名; |
|---|---|
| stack<int> s1; | 创建一个名为s1的空堆栈,s1中的元素类型为int |
| stack<int> s2(s1); | 堆栈s2中包含和堆栈s1一样的元素 |
| stack<int> s2=s1; | 堆栈s2中包含和堆栈s1一样的元素 |
| stack<int> s2(d1); | 堆栈s2中包含和双端队列d1一样的元素,d1的类型为deque |
| queue | queue<类型名> 队列名; |
|---|---|
| queue<int> q1; | 创建一个名为q1的空单端队列,q1中的元素类型为int |
| queue<int> q2(q1); | 单端队列q2中包含和单端队列q1一样的元素 |
| queue<int> q2=q1; | 单端队列q2中包含和单端队列q1一样的元素 |
| queue<int> q2(d1); | 单端队列q2中包含和双端队列d1一样的元素,d1的类型为deque |
其他
| stack | |
|---|---|
| s1.top(); | 返回堆栈的栈顶元素 |
| s1.pop(); | 栈顶元素出栈 |
| s1.push(e); | 元素e入栈 |
| s1.emplace(e); | 元素e入栈 |
| s1.empty(); | 堆栈为空则返回true,否则返回false |
| s1.size(); | 堆栈的大小 |
| s1.swap(s2); | 交换s1、s2的元素 |
| queue | |
|---|---|
| q1.front(); | 返回单端队列的队首元素 |
| q1.back(); | 返回单端队列的队尾元素 |
| q1.pop(); | 队首元素出队 |
| q1.push(e); | 添加元素e到队尾 |
| q1.emplace(e); | 添加元素e到队尾 |
| q1.empty(); | 单端队列为空则返回true,否则返回false |
| q1.size(); | 单端队列的大小 |
| q1.swap(q2); | 交换q1、q2的元素 |
容器适配器不支持迭代器
| 容器 | 迭代器 |
|---|---|
| vector | 随机访问 |
| string | 随机访问 |
| deque | 随机访问 |
| array | 随机访问 |
| list | 双向 |
| forward_list | 单向 ,只能++不能- - |
| set/multiset | 双向 |
| map/multimap | 双向 |
| unordered_set/unordered_multiset | 双向 |
| unordered_map/unordered_multimap | 双向 |
| stack | 不支持迭代器 |
| queue | 不支持迭代器 |
| priority_queue | 不支持迭代器 |
三、程序举例
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int main() {
deque<int> d1{
1,2 }; // 创建一个双端队列,元素为1,2
stack<int> s1(d1); // 用双端队列来初始化栈
stack<int> s2; // 创建一个空栈
int a = s1.top(); // 栈顶元素a=2
s1.pop(); // 元素2出栈
s1.push(3); // 3入栈,s1元素为1,3
int size = s1.size(); // s1的大小为2
s1.swap(s2); // 交换s1和s2的所有元素
queue<int> q1(d1); // 用双端队列来初始化单端队列
queue<int> q2; // 创建一个空队列
int f = q1.front(); // 队首元素f=1
int b = q1.back(); // 队尾元素b=2
q1.pop(); // 队首元素出队
q2.push(3); // 入队,队列元素为2,3
int size2 = q1.size(); // 队列的大小
q1.swap(q2); // 交换q1和q2所有元素
}
边栏推荐
- Sword finger offer 42 Maximum sum of successive subarrays
- js数组的索引为何不能用负数
- Cloud box is deeply convinced to create a smart dual-mode teaching resource sharing platform for Nanjing No. 1 middle school
- 1161 Merging Linked Lists
- Give up Visio, this drawing tool is really fragrant!
- 页面嵌入iframe 点击浏览器后退问题
- 二叉树的遍历及相关知识
- 如何迁移virtualbox 的虚拟机到hype-v
- 回调函数详解
- Business logic design of physical warehouse and virtual warehouse in middle inventory
猜你喜欢

Influence of steam education on domestic college students

Linux Installation mysql8.0.25

数据在内存中的存储方式(C语言)

mingw-w64、msys和ffmpeg的配置与编译

WPF Command指令和INotifyPropertyChanged

Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)

Easy EDA #学习笔记09# | ESP32-WROOM-32E模组ESP32-DevKitC-V4开发板 一键下载电路

Copy and paste of idea without escape

xml dtd 记录

English语法_形容词比较级 - 3级变化
随机推荐
嵌入式实时系统线程的副作用
2.17 haas506 2.0开发教程-system(仅支持2.2以上版本)
2022-01-12: give a positive array arr, length N, subscript 0~n-1, a
QT设计师无法修改窗口大小,无法通过鼠标拖动窗口改变大小的解决方案
MySQL function
Set tensorflow1 X to pytorch
Leetcode notes: Weekly contest 298
Numerical calculation method chapter7 Calculating eigenvalues and eigenvectors of matrices
小白投资理财必看:图解基金买入与卖出规则
C language obtains second, millisecond, subtle and nanosecond timestamps
XXL-SSO 实现SSO单点登录
Problem: when the attribute in the data object (defined data) in the access component is also the attribute in the object object, an error is reported
Linux安装mysql8.0.25
QT method of compiling projects using multithreading
system 权限程序不能访问sd卡问题
Measurement principle and thickness measurement mode of spectral confocal
core. What is JS ---kalrry
中台库存中的实仓与虚仓的业务逻辑设计
Gridsearchcv (grid search), a model parameter adjuster in sklearn
Haas506 2.0 development tutorial -sntp (only versions above 2.2 are supported)