当前位置:网站首页>STL adapter stack and queue
STL adapter stack and queue
2022-07-23 06:54:00 【Li Fengxi】
One 、 The purpose of this article is
1、 Introduce STL One of the six components -- Adapter
2、 use list and vector Realization stack and queue
Two 、 What is an adapter ?
An adapter is a design pattern ( Design patterns are a set of things that are used repeatedly 、 Most people know that 、 Catalogued 、 Total code design experiencejunction ), This pattern is to convert the interface of one class into another interface that the customer wants .Simply speaking , Adapter is actually a good design method when we write code . For example, we need to write a stack , We can use it directly vector perhaps list Do the bottom layer to realize .
3、 ... and 、 Realization stack
template<class T, class Con = deque<T>> class stack { public: void push(const T& x) { _c.push_back(x); } void pop() { assert(!empty()); _c.pop_back(); } bool empty() { return _c.empty(); } size_t size() { return _c.size(); } const T& top() { return _c.back(); } private: Con _c; };
Four 、 Implementation queue
template<class T, class Con = deque<T>> class queue { public: void push(const T& x) { _c.push_back(x); } void pop() { _c.pop_front(); } size_t size() { return _c.size(); } bool empty() { return _c.empty(); } const T& front() { return _c.front(); } const T& back() { return _c.back(); } private: Con _c; };
5、 ... and 、 It can be used vector Be the bottom of the queue ?
void test1() { queue<int, vector<int>> s; s.push(1); s.push(2); s.push(3); s.push(4); while (!s.empty()) { cout << s.front() << endl; s.pop(); } }
answer :vector Can't do queue The bottom of the , because queue need pop_front(), and vector There is no such interface , Only erase Interface .
6、 ... and 、 Why? STL Inside stack and queue It's all default deque Such a container ?
Here we need to introduce the following deque,deque yes double queue Abbreviation , It is often referred to as a double ended queue , Because its insertion and deletion efficiency at both ends is very high , What the queue and stack need is a container with high efficiency of inserting and deleting at both ends , therefore deque Often do stack and queue The bottom container of . About deque How to design and implement the bottom layer of , I will write a special article to explain deque Design and implementation .
边栏推荐
- Codeforces Round #808 (Div. 2) C,D Codeforces Round #809 (Div. 2) C
- TCP四次挥手
- BGP邦联实验
- One type and six methods of urllib
- H2O R language construction
- At the forefront of the times, Huawei aims at the wind and sea of digital finance
- The synchronized lock that I have been wondering about is so simple!
- 第八章 使用时序数据
- How about opening an account of Huatai Securities ETF fund? Is it safe
- LeetCode_单调栈_中等_316.去除重复字母
猜你喜欢
随机推荐
传奇私服GOM引擎启动M2提示:[X-FKGOM] 已经加载成功卡住的怎么处理?
MySq 数据库约束
ABAP ALV summary
2022备忘录
urllib的一个类型和六个方法
100 行代码透彻解析 RPC 原理
第六章 更多监督训练
OSPF related content
一直想不明白的synchronized锁竟如此简单!
Rllib学习 - [4] - AlgorithmConfig详细介绍 [Pytorch版]
强化学习第二章习题
Gom引擎Key.lic配套的X-FKGOM授权启动
Day27作业
常用正则表达式最强整理速查手册(荣耀典藏版)
Zhongang Mining: fluorite is rich in color and has great aesthetic value
安装和登录安装
shp建筑轮廓数据修复,三维城市白膜数据制作
472-82(22、165、39、剑指 Offer II 078、48. 旋转图像)
BGP Confederacy experiment
How to package your project and let other users install it through pip



![swing-[MyNote]-实现像IDEA一样的定位scroll from souce功能](/img/ee/53aae922d7a4b3df3871a3e997cc57.png)








