classMyStack { private: vector<int> data; // store elements public: /** Insert an element into the stack. */ voidpush(int x){ data.push_back(x); } /** Checks whether the queue is empty or not. */ boolisEmpty(){ return data.empty(); } /** Get the top item from the queue. */ inttop(){ return data.back(); } /** Delete an element from the queue. Return true if the operation is successful. */ boolpop(){ if (isEmpty()) { returnfalse; } data.pop_back(); returntrue; } };
intmain(){ MyStack s; s.push(1); s.push(2); s.push(3); for (int i = 0; i < 4; ++i) { if (!s.isEmpty()) { cout << s.top() << endl; } cout << (s.pop() ? "true" : "false") << endl; } }