stack的模拟实现

#include<iostream>
using namespace std;
#include<deque>

//库函数stack的底层结构为deque
//模板参数: T--数据类型    Container--底层数据结构
template<class T, class Container = deque<T>>
class stack
{
public:
	stack()
		:con()
	{}

	void push(const T& x)
	{
		con.push_back(x);
	}
	void pop()
	{
		con.pop_back();
	}
	size_t size()
	{
		return con.size();
	}
	T& top()
	{
		return con.back();
	}
	const T& top()const
	{
		return con.back();
	}
	bool empty()
	{
		return con.empty();
	}
private:
	Container con;
};

void testStack()
{
	stack<int> s;
	if (s.empty())
	{
		cout << s.size() << endl;
	}
	s.push(5);
	s.push(4);
	s.push(3);
	s.push(2);
	s.push(1);
	cout << s.size() << endl;

	cout << s.top() << endl;

	s.pop();
	s.pop();
	cout << s.size() << endl;
	cout << s.top() << endl;

}

int main()
{
	testStack();
	return 0;
}
发布了67 篇原创文章 · 获赞 15 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43746320/article/details/102749009