C++ 容器适配器_栈_队列_双端队列

C++初级语法

容器适配器

  • 容器适配器:一种适配器模式的类模板,STL核心组件之一,容器适配器的核心也是数据结构,容器适配器内 也包含操作其内部数据结构对应的方法,用户可以直接使用 容器适配器来 提高编程效率
    • 容器适配器不同于容器,容器适配器是通过借助容器的基础 来实现的 以数据结构为核心的 类模板
    • CppSTL容器适配器部分主要通过类模板来实现
  • 适配器模式:把已有的 对象和对象的接口 封装成想要的新对象

stack<[具体类型]> 对应的类模板

  • STL配接器/适配器中以 栈 为核心的类模板
    • 类模板:template<class T,class Container=deque<T>> class stack;
      • 这里class T,表示用户可以用 任意类型 去通过类模板形成 想要的 stack<[具体类型]>类代码
      • 这里class Container,表示用户可以手动指定 容器适配器 具体底层借助 哪个stl容器->Container可以被用户采用为list<[具体类型]>vector<[具体类型]>也可以是deque<[具体类型]>
        • 这里官方默认采用deque<T>来作为Container
        • 这里模版的类型参数的缺省值也是从右往左给
  • 使用stack类模板 需包含<stack>头文件
    • stack是类模板名,stack<[具体类型]>是具体类名
  • 注意:这里stack<[具体类型]>对应的类 不支持迭代器,因为栈结构无需迭代器

stack 适配器中 Container 的泛型模式 来做到不同的容器 都可以满足 适配器模式

php 复制代码
vector<[具体类型]>    list<[具体类型]>    list<[具体类型]>
      |_________________|__________________|
                        |
                 _______|________
                |Container _con  |
                |________________|

std::stack<具体类型> 标识符,对应标识符下对象的底层逻辑

这里打算采用stl容器中的vector容器 来实现stack容器适配器,实际官方使用的是deque容器,不过逻辑是一样的

cpp 复制代码
//stack<int,vector<int>> st,这里Container接收vector<int>

 _______________________________________________________________________________    ________________________
|  __________________________________:Container _con;(Container=vector<int>)    |  | 1 | 2 | 3 |   |illicit |
| |iterator _start;------------------|------------------------------------------|->|___|___|___|___|________|
| |iterator _finish;(_start+3)-------|------------------------------------------|----------------^      ^
| |iterator _endofstorage;(_start+4) |                                          |                       |
| |_________________|________________|                                          |                       |
|___________________|___________________________________________________________|                       |
                    |___________________________________________________________________________________|

stack<[具体类型]> 对应的构造函数

stack(),默认构造

cpp 复制代码
Container=vector<[具体类型]> 为例
_______________________________________________________________________________
|  __________________________________:Container _con;(Container=vector<int>)    |
| |iterator _start;------------------|------------------------------------------|------>@@@
| |iterator _finish;(_start)---------|------------------------------------------|------>@@@
| |iterator _endofstorage;(_start)---|------------------------------------------|------>@@@
| |__________________________________|                                          |
|_______________________________________________________________________________|

Container=list<[具体类型]> 为例
      _____________________________________________________________________________
     |            ______________________:Container _con;(Container=vector<Type>)   |
     |           |list<Type>            |                                          |
     |           |ListNode<Type>* _head;|                                          |
     |           |________________|_____|                                          |
     |____________________________|________________________________________________|
                                  |       ____
                                  |      |    |
 _________________________________V______|____V___________
|ListNode<Type>illicit                   |          Type()|
|ListNode<Type>* _next;  ListNode<Type>* _prev; Type data;|
|________________|________________________________________|
                 |    ^
                 |____|

stack<[具体类型]> 对应的成员函数

bool empty(),模拟栈中是判空操作,空返回真,反之返回假,底层本质:执行当前statck<[具体类型]>对应对象._con.empty()操作 来模拟栈判空操作.
size_t size(),模拟返回栈内已用空间个数,底层本质:执行当前statck<[具体类型]>对应对象._con.size()操作 来模拟返回栈内已用空间个数操作.
const T& top(),模拟返回对象为栈顶元素操作,此函数调用只具有栈顶元素的只读操作,底层本质:执行当前statck<[具体类型]>对应对象._con.back()操作
void push(const T& x),模拟入栈操作,在栈顶插入 x 数据,底层本质:执行当前statck<[具体类型]>对应对象._con.push_back(x)操作 来模拟栈操作
void pop(),模拟出栈操作,底层本质:执行当前statck<[具体类型]>对应对象._con.pop_back()操作 来模拟出栈操作.
swap.

queue<[具体类型]> 类模板

  • STL配接器/适配器中以 队列 为核心的类模板
    • 类模板:template<class T,class Container=deque<T>> class queue;
      • 这里class T,表示用户可以用 任意类型 去通过类模板形成 想要的 queue<[具体类型]>类代码
      • 这里class Container,表示用户可以手动指定 容器适配器 具体底层借助 哪个stl容器->Container可以被用户采用为list<[具体类型]>deque<[具体类型]>
        • 这里官方默认采用deque<T>来作为Container
        • 这里模版的类型参数的缺省值也是从右往左给
  • 使用queue类模板 需包含<stack>头文件
    • queue是类模板名,queue<[具体类型]>是具体类名
  • 注意:这里queue<[具体类型]>对应的类 不支持迭代器,因为栈结构无需迭代器

queue 适配器中 Container 的泛型模式 来做到不同的容器 都可以满足 适配器模式

php 复制代码
vector<[具体类型]>    list<[具体类型]>    list<[具体类型]>
      |_________________|__________________|
                        |
                 _______|________
                |Container _con  |
                |________________|

std::stack<具体类型> 标识符,对应标识符下对象的底层逻辑

这里打算采用stl容器中的list容器 来实现stack容器适配器,实际官方使用的是deque容器,不过逻辑是一样的

cpp 复制代码
//stack<int,list<int>> st,这里Container接收list<int>
      _____________________________________________________________________________
     |            ______________________:Container _con;(Container=vector<int>)   |
     |           |list<int>            |                                          |
     |           |ListNode<int>* _head;|                                          |
     |           |size_t _size         |:0                                        |
     |           |________________|____|                                          |
     |____________________________|_______________________________________________|
                                  |       ____
                                  |      |    |
 _________________________________V______|____V_________
|ListNode<int>illicit                   |          int()|
|ListNode<int>* _next;  ListNode<int>* _prev; int data; |
|________________|______________________________________|
                 |    ^
                 |____|

queue<[具体类型]> 对应的构造函数

queue(),默认构造

cpp 复制代码
Container=list<[具体类型]> 为例
      _____________________________________________________________________________
     |            ______________________:Container _con;(Container=list<Type>)     |
     |           |list<Type>            |                                          |
     |           |ListNode<Type>* _head;|                                          |
     |           |size_t _size          |:0                                        |
     |           |________________|_____|                                          |
     |____________________________|________________________________________________|
                                  |       ____
                                  |      |    |
 _________________________________V______|____V___________
|ListNode<Type>illicit                   |          Type()|
|ListNode<Type>* _next;  ListNode<Type>* _prev; Type data;|
|________________|________________________________________|
                 |    ^
                 |____|

queue<[具体类型]> 对应的成员函数

bool empty(),模拟队列中是判空操作,空返回真,反之返回假,底层本质:执行当前queue<[具体类型]>对应对象._con.empty()操作 来模拟队列判空操作.
size_t size(),模拟返回队列内已用空间个数,底层本质:执行当前queue<[具体类型]>对应对象._con.size()操作 来模拟返回队列内已用空间个数操作.
void push(const T& x),模拟入队列操作,在队列尾插入 x 数据,底层本质:执行当前queue<[具体类型]>对应对象._con.push_back(x)操作 来模拟入队列操作
void pop(),模拟出队列操作,底层本质:执行当前queue<[具体类型]>对应对象._con.pop_back()操作 来模拟出队列操作.
T& back()模拟返回对象为队列尾部元素操作.
const T& back()模拟返回对象为队列尾部元素操作.
T& front()模拟返回对象为队列头部元素操作.
const T& front()模拟返回对象为队列头部元素操作.

deque<[具体类型]>对应的类模板

  • STL配接器/适配器中以 双端队列 为核心的类模板
    • 类模板:template<class T,class Container=deque<T>> class deque;
      • 这里class T,表示用户可以用 任意类型 去通过类模板形成 想要的 deque<[具体类型]>类代码
  • 使用stack类模板 需包含<stack>头文件
    • stack是类模板名,stack<[具体类型]>是具体类名
  • 我们只需要了解即可对于deque这个容器即可
cpp 复制代码
deque<int> dq;
                              _________________________________________________
                             | deque<int>                                      |
                             | size_t _map_capacity = 4;                       |
                             | DequeIterator _start;                           |:dp.start()
                             | DequeIterator _finish;                          |:dp.end()
                             | int** _map;                                     |
                             |_______|_________________________________________|
                                     |
                                _____V______________________________________ :map数组
                               | int* ptr0 |int* ptr1 |int* ptr2 |          |
                               |___________|__________|__________|__________|
                                 |                  |     |
                               __V________________  |    _V______________
                              | 10 | 20 | 30 | 40 | |   | 90 |   |   |   |
                              |____|____|____|___ | |   |____|___|___|___|
                                                   _V_________________
                                                  | 50 | 60 | 70 | 80 |
                                                  |____|____|____|____|

deque<[具体类型]>对应的迭代器

cpp 复制代码
deque<int> dq;
                              _________________________________________________
                             | deque<int>                                      |
                             | size_t _map_capacity = 4;                       |
                             | DequeIterator _start;                           |:dp.start()
                             | DequeIterator _finish;                          |:dp.end()
                             | int** _map;                                     |
                             |_______|_________________________________________|
                                     |                                           ______________ :dq.end()
                                     |                                          |DequeIterator |
                                     |                                          |int* first____|___________
                                     |          ___________________________     |int* cur______|________   |
                                     |         | 50 | 60 | 70 | 80 |illicit|    |int* last_____|__      |  |
                                     |         |____|____|____|____|_______|    |int** node    |  |     |  |
                                     |          ^                               |________|_____|  |     |  |
         ____________:dq.begin()     |          |               _________________________|        |     |  |
        |DequeIterator|         _____V__________|_______________V_____________ :map               |     |  |
        |int** node---|------->| int* ptr0  |int* ptr1 |int* ptr2 |         |                     |     |  |
        |int* first___|____    |________|___|__________|________|_|_________|                     |     |  |
        |int* last____|_   |      ______|                       |                                 |     |  |
        |int* cur     | |  |   __V________________________     _V______________________           |     |  |
        |______|______| |  |  | 10 | 20 | 30 | 40 |illicit|   | 90 |   |   |   |illicit|          |     |  |
               |        |  |  |____|____|____|____|_______|   |____|___|___|___|_______|          |     |  |
               |        |  |____^              ^       ^         ^   ^       ^____________________|     |  |
               |        |______________________|       |         |   |__________________________________|  |
               |_______________________________________|         |_________________________________________|

双端队列

  • 一种兼具数组和链表功能的数据结构,但是在效率上又远不如链表和数组
    • 双端队列的只有头插和尾插效率是比数组和链表高的
相关推荐
卷无止境5 分钟前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境43 分钟前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴1 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境3 天前
C++ 的Eigen 库全解析
c++
卷无止境3 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴3 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18005 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴5 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨6 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint45610 天前
C++进阶(1)——前景提要
c++