当前位置:网站首页>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;
};
边栏推荐
- 我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
- Driving the efficient growth of the manufacturing industry, UFIDA u9 cloud is constantly improving the password behind it
- Meet webassembly again
- 极致效率,云原生数据库TDSQL-C安身立命的根本
- Common operations in Visual Studio development
- TDengine 连接器上线 Google Data Studio 应用商店
- “软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启
- Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!
- GBASE现身说 “库” 北京金融科技产业联盟创新应用专委会专题培训
- Database connection pool: Code Directory
猜你喜欢

架构师之路,从「存储选型」起步

Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node

那些没考上大学的人,后来过的怎样

C语言学习-17-函数作为参数传入函数

After 100 days, Xiaoyu built a robot communication community!! Now invite moderators!

SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)

Tdengine connector goes online Google Data Studio store

Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?

得物App数据模拟平台的探索和实践

On the routing tree of gin
随机推荐
乱解码nlp
Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
Method of using inout signal in Verilog
建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!
极致效率,云原生数据库TDSQL-C安身立命的根本
GBASE现身说 “库” 北京金融科技产业联盟创新应用专委会专题培训
C语言学习-17-函数作为参数传入函数
关于 GIN 的路由树
How MySQL modifies the storage engine to InnoDB
我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
Application of mongodb in Tencent retail premium code
Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?
向量6(继承)
华为机器学习服务银行卡识别功能,一键实现银行卡识别与绑定
基础版现在SQL分析查询不能用了吗?
模板特例化 template<>
Ml notes matrix fundamental, gradient descent
鸿世电器冲刺创业板:年营收6亿 刘金贤股权曾被广德小贷冻结
百行代码实现基于Redis的可靠延迟队列
New load balancing webclient CRUD