当前位置:网站首页>Simulation of stack and queue
Simulation of stack and queue
2022-06-22 15:58:00 【Stock god.】
stack The main member variable of
stack Is an adapter container .
Adapter : Create a new container from an existing container , For example vector Can be realized stack.
We don't use it directly here vector, Instead, a template is used Container, In this way, we can pass different containers to achieve stack( The default container is deque), This is not limited to the use of vector To achieve .
template<class T,class Container=deque<T>>
class stack
{
public:
// Various member functions
//....
private:
//vector<T> _CON;// Don't write directly vector, If you write like this , You can only implement the array stack , We should write a template parameter , This allows you to pass in different containers , Implement different stacks
Container _con;// Write constructor for , Custom types call default constructors .
};
empty
Judge stack Is it empty
bool empty()const
{
return _con.empty();
}
size
return stack Size
size_t size()const
{
return _con.size();
}
push
Insert an element , the reason being that stack, So call _con Tail interpolation method of
void push(const T& val)
{
_con.push_back(val);
}
top
Back to top of stack element
const T& top()const
{
return _con.back();
}
pop
Out of the stack
void pop()
{
_con.pop_back();
}
queue The main member variable of
queue It is also an adapter container , The container used is also a template parameter , Default transmission deque.
template<class T,class Container=deque<T>>
class queue
{
public:
// Various member functions
//........
private:
Container _con;
};
queue The simulation implementation and stack similar , Here we will give the whole code directly .
template<class T,class Container=deque<T>>
class queue
{
public:
bool empty() const
{
return _con.empty();
}
size_t size()const
{
return _con.size();
}
const T& front()const
{
return _con.front();
}
const T& back() const
{
return _con.back();
}
void push(const T& x)
{
_con.push_back(x);
}
void pop()
{
_con.pop_front();
}
private:
Container _con;
};
边栏推荐
- 社区文章|MOSN 构建 Subset 优化思路分享
- 向量1(类和对象)
- 【山大会议】WebRTC基础之用户媒体的获取
- 推進兼容適配,使能協同發展 GBase 5月適配速遞
- [Shangshui Shuo series] day three - VIDEO
- Scala语言学习-04-函数作为参数传入函数-函数作为返回值
- C language learning -18-makefile file writing examples and how to generate and call dynamic libraries
- 程序替换函数
- js中const定义变量及for-of和for-in
- New load balancing webclient CRUD
猜你喜欢

pymssql模块使用指南

华为机器学习服务银行卡识别功能,一键实现银行卡识别与绑定

Exploration and practice of dewu app data simulation platform

On the routing tree of gin

推进兼容适配,使能协同发展 GBase 5月适配速递

Wechat applet avatar pendant production
![[译文] 弥合开源数据库和数据库业务之间的鸿沟](/img/e5/f89a8f3e2e9034f557ea3e76f37f81.jpg)
[译文] 弥合开源数据库和数据库业务之间的鸿沟

“软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启

快速玩转CI/CD图形化编排

(pytorch advanced path 2) word embedding and position embedding
随机推荐
Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
Promouvoir l'adaptation compatible et permettre le développement collaboratif du Service Express adaptatif gbase en mai
排序之归并排序
C语言学习-18-makefile文件编写例子以及如何生成、调用动态库
问一下想获取到sqlserver的start_lsn有好的办法吗?
[leetcode] 9. Palindromes
向量6(继承)
TDengine 连接器上线 Google Data Studio 应用商店
Rosbag use command
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
Exploration and practice of dewu app data simulation platform
String的模拟实现
大佬们 2.2.1cdc 监控sqlsever 只能拿到全量的数据 后期增量的数据拿不到 咋回事啊
Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express
又可以这样搞nlp(分类)
架构师之路,从「存储选型」起步
米哈游六月社招火热开启!500+岗位,超多HC,就在这个夏天(附内推方式)
DDD understanding of Domain Driven Design
js中const定义变量及for-of和for-in
再次认识 WebAssembly