#include<iostream>
#include<list>
#include<vector>
using std::cout;
using std::cin;
namespace king{
template<class T,class Container>//Container
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;
};
}
int main() {
king::stack<int, std::vector<int>> st;
king::stack<int, std::list<int>> sq;
st.push(1);
st.push(1);
st.push(1);
st.push(1);
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
std::cout<<std::endl;
sq.push(1);
sq.push(1);
sq.push(1);
sq.push(1);
while (!sq.empty()) {
cout << sq.top() << " ";
sq.pop();
}
return 0;
}
模拟STL 实现stack 容器适配器
最新推荐文章于 2024-08-17 23:59:05 发布