C++ 浅谈之 STL Stack,Queue
创始人
2024-05-24 22:04:01
0

C++ 浅谈之 STL Stack,Queue

HELLO,各位博友好,我是阿呆 🙈🙈🙈

这里是 C++ 浅谈系列,收录在专栏 C++ 语言中 😜😜😜

本系列阿呆将记录一些 C++ 语言重要的语法特性 🏃🏃🏃

OK,兄弟们,废话不多直接开冲 🌞🌞🌞


一 🏠 Stack

stack 先进后出,允许添加、删除、取得顶端元素。不允许遍历,入栈 push,出栈 pop 👦👦👦


stack 定义

deque双向开口,封闭头端开口,便形成了stack,源码如下 👇👇👇

template  >
class stack {friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);
public:typedef typename Sequence::value_type value_type;typedef typename Sequence::size_type size_type;typedef typename Sequence::reference reference;typedef typename Sequence::const_reference const_reference;
protected:Sequence c; // 底层容器
public:bool empty() const { return c.empty(); }size_type size() const { return c.size(); }reference top() { return c.back(); }const_reference top() const { return c.back(); }// deque 是两头可进出,stack 是末端进,末端出(所以后进者先出)void push(const value_type& x) { c.push_back(x); }void pop() { c.pop_back(); }
};template 
bool operator==(const stack& x, const stack& y) {return x.c == y.c;
}template 
bool operator<(const stack& x, const stack& y) {return x.c < y.c;
}

迭代器

stack 不支持遍历,也不提供迭代器 🐋🐋🐋


List 作为底层容器

list 双向开口,一样可形成一个 stack ,代码如下 👇👇👇

#include 
#include 
#include 
#include 
using namespace std;
int main() {stack > istack;
}

二 🏠 Queue

queue 先进先出,允许底部添加、顶部取出、不允许遍历。入队列称为 push,出队列称为 pop 👨‍🚀👨‍🚀👨‍🚀

Queue 定义

deque 为底部结构并封闭其底端出口和前端入口,便形成了queue,源码如下 👇👇👇

template  >
class queue {friend bool operator== __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);friend bool operator< __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);
public:typedef typename Sequence::value_type value_type;typedef typename Sequence::size_type size_type;typedef typename Sequence::reference reference;typedef typename Sequence::const_reference const_reference;
protected:Sequence c; // 底层容器
public:bool empty() const { return c.empty(); }size_type size() const { return c.size(); }reference front() { return c.front(); }const_reference front() const { return c.front(); }reference back() { return c.back(); }const_reference back() const { return c.back(); }// deque 是两头可进出,queue 是末端进,前端出(所以先进者先出)。void push(const value_type& x) { c.push_back(x); }void pop() { c.pop_front(); }
};template 
bool operator==(const queue& x, const queue& y) {return x.c == y.c;
}template 
bool operator<(const queue& x, const queue& y) {return x.c < y.c;
}

迭代器

queue 不支持遍历,也不提供迭代器 🐳🐳🐳


List 作为底层容器

list 双向开口,一样可形成一个 queue ,代码如下 👇👇👇

#include 
#include 
#include 
#include 
using namespace std;
int main() {queue > iqueue;
}

三 🏠 结语

身处于这个浮躁的社会,却有耐心看到这里,你一定是个很厉害的人吧 👍👍👍

各位博友觉得文章有帮助的话,别忘了点赞 + 关注哦,你们的鼓励就是我最大的动力

博主还会不断更新更优质的内容,加油吧!技术人! 💪💪💪

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...