头文件Stack.h
#ifndef STACK_H
#define STACK_H
#include <exception>
#include <deque>
using namespace std;
class ReadEmptyStackError : public exception
{
public:
virtual const char * what() const throw()
{
return "error: stack is empty!!";
}
private:
};
template<class T>
class Stack
{
public:
void push(const T& elem)
{
c.push_back(elem);
}
bool empty() const
{
return c.empty();
}
T pop()
{
if (empty())
{
throw ReadEmptyStackError();
}
T elem(c.back());
c.pop_back();
return elem;
}
T& top()
{
if (empty())
{
throw ReadEmptyStackError();
}
return c.back();
}
private:
std::deque<T> c;
};
#endif
源文件main.cpp:
#include <iostream>
#include "Stack.h"原文地址:http://blog.csdn.net/jean_bai/article/details/45939009