//헤더파일
#ifndef __STACK_HPP__
#define __STACK_HPP__
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
template < typename T >
class Stack
{
public:
Stack() {};
void Push(const T&);
T Pop() throw(...);
const T& Top() const throw(...);
int Size() const;
bool Empty() const;
private:
vector<T> stack;
};
template < typename T >
void Stack<T>::Push(const T& elem)
{
stack.push_back(elem);
}
template < typename T >
T Stack<T>::Pop() throw(...)
{
if ( Empty() )
{
string exception = "Stack Empty.";
throw(exception);
}
T ret = stack.back();
stack.pop_back();
return ret;
}
template < typename T >
const T& Stack<T>::Top() const throw(...)
{
if ( Empty() )
{
string exception = "Stack Empty.";
throw(exception);
}
return stack.back();
}
template < typename T >
int Stack<T>::Size() const
{
return stack.size();
}
template < typename T >
bool Stack<T>::Empty() const
{
return stack.empty();
}
#endif
==================================================================
// Exam.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"
#include "Stack.h"
int _tmain(int argc, _TCHAR* argv[])
{
Stack<int> s;
try
{
for ( int i = 0 ; i < 20 ; i++ )
s.Push(i);
for ( int i = 0 ; i <= 20 ; i++ )
{
cout << s.Top() << endl;
s.Pop();
}
}
catch(string str)
{
cerr << str << endl;
}
return 0;
}