模拟STL stack类模板设计实现你的stack类模板,该类需具有入栈,出栈,判栈空,取栈顶元素等功能,并能拷贝构造和赋值;利用该类实现本题要求。
#include
#include
#include
using namespace std;
template
class Stack
{
private:
vector
elems; public: Stack() {} Stack(int x) { elems = vector
(x); } Stack(const Stack &);//拷贝构造函数 void push(T const&);//在尾部插入元素 void pop();//在尾部删除元素 T top() const;//返回顶部元素 bool empty() const;//返回栈内元素是否为空 }; template
Stack
::Stack(const Stack &s) { elems=s.elems; } template
void Stack
::push(T const &elem) //在尾部插入元素 { elems.push_back(elem); } template
void Stack
::pop() //在尾部删除元素 { elems.pop_back(); } template
T Stack
::top() const//返回栈顶元素 { return elems.back(); } template
bool Stack
::empty() const//返回栈内元素是否为空 { return elems.empty(); } template
void Order(T &order)//函数模板 { int n; cin>>n; Stack
s1,s2; int v; T x; while (n--){ cin>>v; switch (v){ case 1: cin>>x; s1.push(x); break; case 2: cin>>x; s2.push(x); break; default: break; } }//输出栈 while (!s1.empty()) { cout<
<<" "; s1.pop(); } cout<