栈和队列作为容器适配器,只需要将容器封装模板,传入什么容器用什么容器就好了
栈
#pragma oncetemplate <class T, class container = vector<T>>
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;
};
队列
#pragma oncetemplate <class T, class container = list<T>>
class queue
{
public:void push(const T& x){_con.push_back(x);}void pop(){_con.pop_front();}T& front(){return _con.front();}T& back(){return _con.back();}size_t size(){return _con.size();}bool empty(){return _con.empty();}private:container _con;
};