とりあえず使ってみる。
「return 0;」が気になるな…
#include <iostream> using namespace std; template <class T> class Stack { private: T *stack; int size; int tos; public: Stack(int size); ~Stack(); bool push(T x); T pop(); bool empty(); }; template <class T> Stack<T>::Stack(int size) : size(size), tos(0) { stack = new T[size]; } template <class T> Stack<T>::~Stack() { delete [] stack; } template <class T> bool Stack<T>::push(T x) { if (tos >= size) { return false; } stack[tos] = x; tos++; return true; } template <class T> T Stack<T>::pop() { if (tos < 1) { return 0; } tos--; return stack[tos]; } template <class T> bool Stack<T>::empty() { return (tos < 1); } int main() { Stack<int> istack(10); for (int i = 0; i < 10; i++) { istack.push(i * i); } while (!istack.empty()) { cout << istack.pop() << ' '; } cout << endl; Stack<char> cstack(26); for (char i = 'a'; i < ('a' + 26); i++) { cstack.push(i); } while (!cstack.empty()) { cout << cstack.pop(); } cout << endl; }
81 64 49 36 25 16 9 4 1 0
zyxwvutsrqponmlkjihgfedcba