Script started on Thu Mar 22 01:58:33 2007 [tsaiwn@ccbsd3] vectorSTK> [tsaiwn@ccbsd3] vectorSTK> cat -n mystk3.h 1 #include 2 using namespace std; 3 template 4 class MyStack { 5 vector x; 6 public: 7 void push(int y) { 8 x.push_back(y); 9 } 10 int top( ) { 11 return x.back( ); 12 } 13 void pop( ) { 14 x.pop_back( ); 15 } 16 bool empty( ) { return x.begin() == x.end(); } 17 }; [tsaiwn@ccbsd3] vectorSTK> cat -n mymain3.cpp 1 // mymain3.cpp; g++ mymain3.cpp ; ./a.out 2 using namespace std; 3 #include "mystk3.h" 4 //注意以下是 1999之後的 C++ 新寫法, 舊法使用 5 #include 6 int main( ) { 7 MyStack x; // 注意這! 8 x.push(880); 9 x.push(770); 10 x.push(53); 11 while(!x.empty()){ 12 cout << x.top(); x.pop(); 13 } 14 cout << endl; 15 } [tsaiwn@ccbsd3] vectorSTK> g++ mymain3.cpp [tsaiwn@ccbsd3] vectorSTK> ./a.out 53770880 [tsaiwn@ccbsd3] vectorSTK> exit exit Script done on Thu Mar 22 01:58:58 2007