9:46pm ccbsd3:que4/> g++ testmyq4.cpp 9:46pm ccbsd3:que4/> ./a.out 88077053 9:46pm ccbsd3:que4/> cat -n testmyq4.cpp 1 // testmyq4.cpp -- copyLeft by tsaiwn@csie.nctu.edu.tw 2 // g++ testmyq4.cpp ; ./a.out 3 using namespace std; 4 #include "myq4.h" 5 //注意以下是 1999之後的 C++ 新寫法, 舊法使用 6 #include 7 int main( ){ 8 MyStack x; // 注意這! 9 x.push(880); 10 x.push(770); 11 x.push(53); 12 while(!x.empty()){ 13 cout << x.top(); x.pop(); 14 } 15 cout << endl; 16 } 9:46pm ccbsd3:que4/> cat -n myq4.h 1 //myq4.h -- queue using vector (not GOOD) 2 #ifndef __MYQ4__ 3 #define __MYQ4__ 4 #include 5 using namespace std; 6 template 7 class MyStack : vector { // actually is a queue 8 public: 9 void push(const T& y) { 10 MyStack::push_back(y); 11 } 12 T top( ) { 13 return MyStack::front( ); 14 return vector::front( ); // OK too 15 //return this -> front( ); // this is also OK 16 } 17 void pop( ) { 18 MyStack::erase( MyStack::begin( ) ); 19 } 20 bool empty( ) { return MyStack::begin() == MyStack::end(); } 21 }; 22 #endif 9:46pm ccbsd3:que4/> 9:46pm ccbsd3:que4/> g++ testmyq4.cpp 9:46pm ccbsd3:que4/> ./a.out 88077053 9:46pm ccbsd3:que4/> exit exit