题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
int n ;
cin >> n;
stack<int> st;
for (int i = 0;i < n;i++)
{
string op;
getline(cin,op);
auto it = op.find(' ');
if (it != string::npos)
{
string a = op.substr(0,it);
string data = op.substr(it + 1);
if (a == "push")
{
st.push(stoi(data));
//cout << st.top() << endl;
}
}
else
{
if (op == "pop")
{
if (!st.empty())
{
cout << st.top() << endl;
st.pop();
}
else
{
cout << "error" << endl;
}
}
else if (op == "top")
{
if (!st.empty())
{
cout << st.top() << endl;
}
else
{
cout << "error" << endl;
}
}
}
if(i == n - 1)
{
if (!st.empty())
{
cout << st.top() << endl;
}
else
{
cout << "error" << endl;
}
}
}
}
// 64 位输出请用 printf("%lld")

