类模拟栈

/* 实现一个stack类 */


#include <bits/stdc++.h>

using namespace std;

class My_stack{
private:
    vector<int> s; //数组模拟栈
    int pos = -1;	// -1表示栈空
    
public:
    My_stack(int size) {
        s.resize(size);
    }
    
    My_stack() {
    }
      
    void push(int x){
        s[++pos] = x;
    }
    
    void top (){
        if(pos == -1) {
            cout << "error" << endl;
        }
        else {
            cout << s[pos] << endl;
        }
    }
    
    void pop() {
        if(pos == -1) {
            cout << "error" << endl;
        }
        else {
            cout << s[pos--] << endl;
        }
    }  
   
};


int main(){
    My_stack * st = new My_stack(100005);
    int n, x, top = -1;
    string s;
    cin >> n;
    while(n--){
        cin >> s;
        if(s == "push"){
            cin >> x;
            st->push(x);
        }
        else if(s == "pop"){
           st->pop();
        }
        else if(s == "top"){
          st->top();
        }
    }
    return 0;
}