棧(vector)實現 c++

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

template <class T>
class Stack{
public:
	void Push(const T& key);
	void Pop();
	T& Top();
	bool Empty() const;
private:
	vector<T> c;
};

template<class T> void Stack<T>::Push(const T& key)
{
	c.push_back(key);
}

template<class T> void Stack<T>::Pop()
{
	if (!Empty())
	{
		c.pop_back();
	}
}

template <class T> T& Stack<T>::Top() 
{
	if (!Empty())
	{
		return c.back();
	}
}

template<class T> bool Stack<T>::Empty() const
{
	return c.empty();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章