C++初级语法
容器适配器
- 容器适配器:一种适配器模式的类模板,
STL核心组件之一,容器适配器的核心也是数据结构,容器适配器内 也包含操作其内部数据结构对应的方法,用户可以直接使用 容器适配器来 提高编程效率- 容器适配器不同于容器,容器适配器是通过借助容器的基础 来实现的 以数据结构为核心的 类模板
Cpp中STL容器适配器部分主要通过类模板来实现
- 适配器模式:把已有的 对象和对象的接口 封装成想要的新对象
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| | | |
| | | |____|____|____|____|_______| |____|___|___|___|_______| | | |
| | |____^ ^ ^ ^ ^ ^____________________| | |
| |______________________| | | |__________________________________| |
|_______________________________________| |_________________________________________|
双端队列
- 一种兼具数组和链表功能的数据结构,但是在效率上又远不如链表和数组
- 双端队列的只有头插和尾插效率是比数组和链表高的