当前位置:网站首页>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;
};
边栏推荐
猜你喜欢

ArcGIS JS之 4.23之IIS本地部署与问题解决

C # implements insertion sorting

Quickly play ci/cd graphical choreography

Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour

(pytorch进阶之路二)word embedding 和 position embedding

TDengine 连接器上线 Google Data Studio 应用商店

Advanced thinking on application scenarios of standardization, maximum normalization and mean normalization

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

C language learning -17- function is passed in as a parameter

Meet webassembly again
随机推荐
The difference between nvarchar and varchar
Luogu p2466 [sdoi2008] Sue's small ball solution
DevSecOps: CI/CD 流水线安全的最佳实践
New load balancing webclient CRUD
【山大会议】注册页的编写
高精度计算
Be an we media video blogger, and share the necessary 32 material websites
Hello, big guys. Error reporting when using MySQL CDC for the first time
快速玩转CI/CD图形化编排
各位学弟学妹,别再看教材了,时间复杂度看这篇就好了
ORB_ VI ideological framework
又可以这样搞nlp(分类)
stack和queue的模拟实现
Discover the number of media new users can insert
【newman】postman生成漂亮的测试报告
小程序开发----自定义有效期缓存
Scala language learning-06-differences between name passing parameters, value passing parameters and function passing parameters
Dear students, don't read the textbooks any more. Just read this one for the complexity of time
关于 GIN 的路由树
2020年蓝桥杯省赛真题-走方格(DP/DFS)