当前位置:网站首页>stack和queue的模拟实现
stack和queue的模拟实现
2022-06-22 14:34:00 【股神。】
stack的主要成员变量
stack是一个适配器容器。
适配器:用已有的容器来创建新的容器,例如用vector就可以实现stack。
在这里我们比没有直接用vector,而是用了一个模板Container,这样我们就可以通过传不同的容器来实现stack(默认给的容器是deque),这样就不仅限于只使用vector来实现。
template<class T,class Container=deque<T>>
class stack
{
public:
//各种成员函数
//....
private:
//vector<T> _CON;//不要直接写vector,如果这样写,就只能实现数组栈,我们应该写一个模板参数,这样就可以传入不同的容器,实现不同的栈
Container _con;//为写构造函数,自定义类型调用默认构造函数。
};
empty
判断stack是否为空
bool empty()const
{
return _con.empty();
}
size
返回stack的大小
size_t size()const
{
return _con.size();
}
push
插入一个元素,因为是stack,所以调用_con的尾插方法
void push(const T& val)
{
_con.push_back(val);
}
top
返回栈顶元素
const T& top()const
{
return _con.back();
}
pop
出栈
void pop()
{
_con.pop_back();
}
queue的主要成员变量
queue也是一个适配器容器,所用的容器也是一个模板参数,默认传deque。
template<class T,class Container=deque<T>>
class queue
{
public:
//各种成员函数
//........
private:
Container _con;
};
queue的模拟实现与stack类似,这里就直接给出全部代码。
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;
};
边栏推荐
- What happened to those who didn't go to college
- Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan
- 壹连科技冲刺深交所:年营收14亿 65%收入来自宁德时代
- Devsecops: best practices for ci/cd pipeline security
- Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?
- 对领域驱动设计DDD理解
- 加密市场进入寒冬,是“天灾”还是“人祸”?
- 基础版现在SQL分析查询不能用了吗?
- 乱解码nlp
- Community article | mosn building subset optimization ideas sharing
猜你喜欢

FPGA collects DHT11 temperature and humidity

CVE-2022-0847(提权内核漏洞)

I rely on the sideline to buy a house in full one year: the industry you despise will make a lot of money in the next ten years!

Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?

C# 实现插入排序

HMS Core新闻行业解决方案:让技术加上人文的温度

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

ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程

I took a private job and earned 15250. Is it still necessary to do my main business?

Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!
随机推荐
迷宫问题(BFS记录路径)
The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
对领域驱动设计DDD理解
C# 实现插入排序
基于最小化三维NDT距离的快速精确点云配准
(pytorch进阶之路二)word embedding 和 position embedding
The MIHA tour club in June is hot! 500+ posts, more than HC, just this summer (with internal promotion method)
Scala语言学习-04-函数作为参数传入函数-函数作为返回值
润迈德医疗通过聆讯:年内亏损6.3亿 平安资本是股东
NF RESNET: network signal analysis worth reading after removing BN normalization | ICLR 2021
What are the five characteristics of network security? What are the five attributes?
高精度计算
uni开发微信小程序自定义相机自动检测(人像+身份证)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
希尔排序的简单理解
基础版现在SQL分析查询不能用了吗?
Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
Please, don't be brainwashed. This is the living reality of 90% of Chinese people
What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
Discourse 的信任级别