简单的模板类应用
#include <iostream>
#include <stack>
using namespace std;
template<class T>
class MyQueue
{
public:
void push(T &t);
T front();
void pop();
private:
stack<T> s1;
stack<T> s2;
};
template<class T> void MyQueue<T>::push(T &t)
{
s1.push(t);
}
template<class T> T MyQueue<T>::front()
{
if(s2.empty())
{
if(s1.size()==0) throw;
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
template<class T> void MyQueue<T>::pop()
{
if(s2.empty())
{
if(s1.size()==0) throw;
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
s2.pop();
}
int main()
{
MyQueue<int> mq;
int i;
for(i=0;i<10;i++)
mq.push(i);
while(i--)
{
cout<<mq.front()<<endl;
mq.pop();
}
return 0;
}