stack不需要迭代器 因为 栈是不能随便遍历的 因此方括号也不能用
所以我们在自己实现stack的top函数的时候 是不能直接返回_container[0](链表不能用[])
这个时候就可以用back这个函数了 因为无论是vector还是list都有front和list的接口
stack的实现
#pragma once
#include<vector>
#include<stack>
#include<list>
#include<iostream>
using namespace std;
namespace bit
{
template<class T, class Container = deque<T>>
//deque<T>改成vector<int>或者list<int>也都是可以的
class stack
{
public:
void push(const T& x)
{
_con.push_back(x);
}
void pop()
{
_con.pop_back();
}
T& top()
{
return _con.back();
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Container _con;
};
}
这个地方不用写析构函数 构造函数和拷贝赋值等
因为对于自定义类型 编译器会调用它们的默认构造!
443

被折叠的 条评论
为什么被折叠?



