当前位置:网站首页>STL中stack和queue的使用以及模拟实现
STL中stack和queue的使用以及模拟实现
2022-07-17 12:15:00 【爱编程的小李同学】
STL中stack和queue的使用以及模拟实现
一、 stack的介绍和使用
1、stack的介绍
- stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行元素的插入与提取操作。
- stack是作为容器适配器被实现的,容器适配器即是对特定类封装作为其底层的容器,并提供一组特定的成员函数来访问其元素,将特定类作为其底层的,元素特定容器的尾部(即栈顶)被压入和弹出。
- stack的底层容器可以是任何标准的容器类模板或者一些其他特定的容器类,这些容器类应该支持以下操作:
| empty | 判空操作 |
|---|---|
| back | 获取尾部元素操作 |
| push_back | 尾部插入元素操作 |
| pop_back | 尾部删除元素操作 |
- 标准容器vector、deque、list均符合这些需求,默认情况下,如果没有为stack指定特定的底层容器,默认情况下使用deque。

2、stack的使用
| 函数说明 | 接口说明 |
|---|---|
| stack() | 构造空的栈 |
| empty() | 检测stack是否为空 |
| size() | 返回stack中元素的个数 |
| top() | 返回栈顶元素的引用 |
| push() | 将元素val压入stack中 |
| pop() | 将stack中尾部的元素弹出 |
例如:OJ题最小栈
class MinStack {
public:
void push(int val) {
_st.push(val);
if(_min_st.empty() || val <= _min_st.top()) {
_min_st.push(val);
}
}
void pop() {
if(_st.top() == _min_st.top())
_min_st.pop();
_st.pop();
}
int top() {
return _st.top();
}
int getMin() {
return _min_st.top();
}
private:
stack<int> _st;
stack<int> _min_st;
};
二、queue的介绍和使用
1、queue的介绍
- 队列是一种容器适配器,专门用于在FIFO上下文(先进先出)中操作,其中从容器一端插入元素,另一端提取元素。
- 队列作为容器适配器实现,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从队尾入队列,从队头出队列。
- 底层容器可以是标准容器类模板之一,也可以是其他专门设计的容器类。该底层容器应至少支持以下操作:
| empty | 检测队列是否为空 |
|---|---|
| size | 返回队列中有效元素的个数 |
| front | 返回队头元素的引用 |
| back | 返回队尾元素的引用 |
| push_back | 在队列尾部入队列 |
| pop_front | 在队列头部出队列 |
- 标准容器类deque和list满足了这些要求。默认情况下,如果没有为queue实例化指定容器类,则使用标准容器deque。

2、queue的使用
| 函数声明 | 接口说明 |
|---|---|
| queue() | 构造空的队列 |
| empty() | 检测队列是否为空,是返回true,否则返回false |
| size() | 返回队列中有效元素的个数 |
| front() | 返回队头元素的引用 |
| back() | 返回队尾元素的引用 |
| push() | 在队尾将元素val入队列 |
| pop() | 将队头元素出队列 |
例题:OJ用队列实现栈
class MyStack {
public:
queue<int> q;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
int n = q.size();
q.push(x);
for (int i = 0; i < n; i++) {
q.push(q.front());
q.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int r = q.front();
q.pop();
return r;
}
/** Get the top element. */
int top() {
int r = q.front();
return r;
}
/** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
};
三、模拟实现
1、模拟实现stack
stack.h
#pragma once
#include<deque>
namespace mystack
{
template<class T, class Container = std::deque<T> >
class stack
{
public:
void push(const T& x)
{
_con.push_back(x);
}
void pop()
{
_con.pop_back();
}
const T& top()
{
return _con.back();
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Container _con;
};
}
2、模拟实现queue
queue.h
#pragma once
#include<deque>
namespace myqueue
{
template<class T, class Container = std::deque<T>>
class queue
{
public:
void push(const T& x)
{
_con.push_back(x);
}
void pop()
{
_con.pop_front();
}
const T& front()
{
return _con.front();
}
const T& back()
{
return _con, back();
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Container _con;
};
}
边栏推荐
- 高效理解 FreeSql WhereDynamicFilter,深入了解设计初衷[.NET ORM]
- [200 opencv routines] 233 Moment invariants of regional features
- Microsoft OneNote 教程,如何在 OneNote 中插入数学公式?
- 【MySQL】MySQL的增删查改(进阶)
- Introduction to blender automated modeling
- 【Unity技术积累】实现鼠标画线功能 & LineRenderer
- [Mori city] random talk on GIS data (IV) - coordinate system
- 【东北师范大学】考研初试复试资料分享
- 上學=掙錢?無需繳納學費的神仙院校!
- Huawei ascend910 running yolov3 tutorial
猜你喜欢

【排序】归并排序

Go to school = earn money? Immortal college without paying tuition fees!

Flink entry to practice - stage 5 (processing function)

Huawei wireless device configuration intelligent roaming

为什么磁力变速齿轮会反转?

Software engineering - ranking of majors in Chinese Universities of Software Science

图神经网络的可解释性方法介绍和GNNExplainer解释预测的代码示例

opencv 画黑色矩形,并写上序号

Smart Lang: VMware fixed virtual machine IP address

Good news
随机推荐
Introduction to blender automated modeling
Random talk on GIS data (III)
A multidimensional sequence anomaly detection method based on Grubbs and isolated forest
Blender数字孪生制作教程
2022.07.13 暑假集训 个人排位赛(八)
C语言结构体实现简易通讯录
KunlunBase 线上Meetup等您来~
Huawei ascend910 running yolov3 tutorial
Good news
Three.js基本元素使用
Aller à l'école = gagner de l'argent? L'Académie des fées sans frais de scolarité!
Rasa 3.x 学习系列-Rasa 3.1.5 版本发布
String类型函数传递问题
R语言dplyr包select函数删除dataframe数据中包含指定字符串内容的数据列(drop columns in dataframe)
C# 搭建一个基于.NET5的WPF入门项目
【分离式超图像分类平台】使用深度学习中那些令人兴奋的模型搭建图像分类平台
How to save and exit VIM
The module created by yourself uses CMD to open the report modulenotfounderror: no module named solution
【Unity技术积累】简易计时器 & 协程 & 延时函数
Quick completion guide of manipulator (zero five): resources related to manipulator