STL
-
stack<类型>名称
-
方法
top():栈顶
pop():移除,不返回任何东西
push():写入
empty():判空
不支持sort
注意
- 区分队列和栈的区别
- 附一个字符串转数字的题和代码
- 后缀表达式
class Solution {
public:
long long legalExp(string str) {
// write code here
stack<long long> st;
int i = 0;
while (i < str.size())
{
if (isdigit(str[i]))
{
long long num = 0;
while (isdigit(str[i]))
{
num = num * 10 + str[i] - '0';
i ++;
}
st.push(num);
}
else if (!isdigit(str[i]) && str[i] != '#')
{
long long num2 = st.top(); st.pop();
long long num1 = st.top(); st.pop();
switch(str[i])
{
case '+': st.push(num1 + num2); break;
case '-': st.push(num1 - num2); break;
case '*': st.push(num1 * num2); break;
}
i ++;
}
else i ++;
}
return st.top();
}
};