头文件
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
/*逆波兰表达式的解释器一般是基于堆栈的。
解释过程一般是:操作数入栈;遇到操作符时,操作数出栈,求值,将结果入栈;
当一遍后,栈顶就是表达式的值。*/
int evalRPN(vector<string> &tokens) {
stack<int> res;//创建栈
for(auto x: tokens){//遍历每一个输入,注意是字符串类型
if(x=="+" || x=="-" || x=="*" || x=="/"){//如果当前字符串是操作符
if(res.size()<2) return 0;//如果栈内数字不足两个,即无法进行运算,则返回0
int a=res.top();res.pop();//依次将栈内两个数字出栈进行操作运算
int b=res.top();res.pop();
int c=0;//用c表示每次运算的结果
if(x=="+") c=b+a;
if(x=="-") c=b-a;
if(x=="*") c=b*a;
if(x=="/") c=b/a;
res.push(c);
}
else{//如果当前字符串是数字
res.push(atoi(x.c_str()));
//操作数入栈,注意先将string字符串转为char字符,再转为int数字
}
}
return res.top();
}
};
1.早在C++98标准中就存在了auto关键字,那时的auto用于声明变量为自动变量,自动变量意为拥有自动的生命期
2.c_str()的用法ing,所以函数c_str()就是将C++的string转化为C的字符串数组,
3.auto用法
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (auto n : arr)
cout << n;
4.输入cin<<n
输出cout<<“tmp=”<<tmp<<endl;
5.STL容器vector
6.标准库vector类型使用须要的头文件:#include 。vector 是一个类模板。不是一种数据类型,vector是一种数据类型。Vector的存储空间是连续的,list不是连续存储的。
链接:https://www.nowcoder.com/questionTerminal/22f9d7dd89374b6c8289e44237c70447?answerType=1&f=discussion
来源:牛客网