Script started on Tue Mar 27 12:37:58 2001
[tsaiwn@ccbsd9] vectorSTK> g++ mymain9.cpp 
[tsaiwn@ccbsd9] vectorSTK> ./a.out 
53770880
[tsaiwn@ccbsd9] vectorSTK>
[tsaiwn@ccbsd9] vectorSTK> cat -n mystk9.h
     1 //mystk9.h  --- CopyLeft by tsaiwn@csie.nctu.edu.tw
     2 // implement a stack using a <vector> via Inheritance method
     3 // 注意這版本是以 Inheritance 的方式用 vector, 不是用 contains 的方式
     4 #include <vector>
     5 using namespace std;
     6 template <class T>
     7 class MyStack: vector<T> {    // 注意這 Inheritance
     8 public:
     9    void push(const T& y) {
    10       MyStack<T>::push_back(y);
    11    }
    12    T top( ) {
    13       return MyStack<T>::back( );
    14    }
    15    void pop( ) {
    16       MyStack<T>::pop_back( );
    17    }
    18    bool empty( ) 
    19     { return MyStack<T>::begin() == MyStack<T>::end(); }
    20 };

[tsaiwn@ccbsd9] vectorSTK> cat -n mymain9.cpp
     1 // mymain9.cpp -- CopyLeft by tsaiwn@csie.nctu.edu.tw
     2 // g++ mymain9.cpp  ; ./a.out
     3 #include "mystk9.h"
     4 // mystk9.h 是用 Inheritance 方式把 vector "擴充" 成 stack
     5 //注意以下是 1999之後的 C++ 新寫法, 舊法使用 <iostream.h>
     6 #include <iostream>
     7 using namespace std;
     8 int main( ){
     9    MyStack <int> x;    // 注意這!
    10    x.push(880);
    11    x.push(770);
    12    x.push(53);
    13    while(!x.empty()){
    14       cout << x.top();  x.pop();
    15    }
    16    cout << endl;
    17 }

[tsaiwn@ccbsd9] vectorSTK> g++ mymain9.cpp
[tsaiwn@ccbsd9] vectorSTK> ./a.out 
53770880
[tsaiwn@ccbsd9] vectorSTK> exit
exit

Script done on Tue Mar 27 12:38:58 2001