#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
using namespace std;
template <class T>
class Stack {
private:
vector<T> element; // elements
public:
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
bool empty() const{ // return true if empty.
return element.empty();
}
};
template <class T>
void Stack<T>::push (T const& item) {
element.push_back(item);
}
template <class T>
void Stack<T>::pop () {
if (element.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
element.pop_back();
}
template <class T>
T Stack<T>::top () const
{
if (element.empty()) {
throw out_of_range("Stack<>::top(): empty stack");
}
return element.back();
}
int main()
{
try {
Stack<int> myIntegerStack; // stack of ints
Stack<string> myStringStack; // stack of strings
myIntegerStack.push(10);
cout << myIntegerStack.top() <<endl;
myStringStack.push("Messi");
cout << myStringStack.top() << std::endl;
myStringStack.pop();
}
catch (exception const& ex) {
cerr << "Exception: " << ex.what() <<endl;
return -1;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHN0ZGV4Y2VwdD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnRlbXBsYXRlIDxjbGFzcyBUPgpjbGFzcyBTdGFjayB7IAoJcHJpdmF0ZTogCiAgICB2ZWN0b3I8VD4gZWxlbWVudDsgICAgIC8vIGVsZW1lbnRzIAoKCXB1YmxpYzogCiAgICB2b2lkIHB1c2goVCBjb25zdCYpOyAgCSAgLy8gcHVzaCBlbGVtZW50IAogICAgdm9pZCBwb3AoKTsgICAgICAgICAgICAgICAvLyBwb3AgZWxlbWVudCAKICAgIFQgdG9wKCkgY29uc3Q7ICAgICAgICAgICAgLy8gcmV0dXJuIHRvcCBlbGVtZW50IAogICAgYm9vbCBlbXB0eSgpIGNvbnN0eyAgICAgICAvLyByZXR1cm4gdHJ1ZSBpZiBlbXB0eS4KICAgICAgICByZXR1cm4gZWxlbWVudC5lbXB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