摘自<<C++ Templates- the complete guide>>
David Vandevoorde Nicolai M.Josuttis 著
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// stack4.hpp
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdexcept>
template <typename T, int MAXSIZE>
class Stack
{
private:
T elems[MAXSIZE];
int numElems;
public:
Stack();
void push(T const&);
void pop();
T top() const;
bool empty() const
{
return numElems == 0;
}
bool full() const
{
return numElems == MAXSIZE;
}
};
template <typename T, int MAXSIZE>
Stack<T, MAXSIZE>::Stack():numElems(0) {}
template <typename T, int MAXSIZE>
void Stack<T, MAXSIZE>::push(T const& elem)
{
if(numElems == MAXSIZE)
{
throw std::out_of_range("Stack<>::push(): stack is full.");
}
elems[numElems] = elem;
++numElems;
}
template <typename T, int MAXSIZE>
void Stack<T, MAXSIZE>::pop()
{
if(numElems <= 0)
{
throw std::out_of_range("Stack<>::pop():empty stack.");
}
--numElems;
}
template <typename T, int MAXSIZE>
T Stack<T, MAXSIZE>::top() const
{
if(numElems<=0)
{
throw std::out_of_range("Stack<>::top():empty stack.");
}
return elems[numElems-1];
}
/******************* stack4test.cpp********************/
#include <iostream>
#include <string>
#include <cstdlib>
#include "stack4.hpp"
int main()
{
try
{
Stack<int, 20> int20Stack;
Stack<int, 40> int40Stack;
Stack<std::string, 40> stringStack;
int20Stack.push(7);
std::cout<< int20Stack.top()<<std::endl;
int20Stack.pop();
stringStack.push("hello");
std::cout<<stringStack.top()<<std::endl;
stringStack.pop();
stringStack.pop();
}
catch(std::exception const &ex)
{
std::cerr<<"Exception: "<<ex.what()<<std::endl;
return EXIT_FAILURE;
}
return 0;
}