C++里string转int常见有atoi和stoi。头文件都是#include
atoi()的参数是const char*, 所以对一个字符串要先c_str()把string转换成const char类型,而stoi()的参数是const string,可直接使用。
#include<cstring>
string str;
int a = atoi(str.c_str());
int b = stor(str);
atoi不会做超出范围的检查,超出上界输出上界,超出下界输出下界。
stoi会做超界检查,超出范围报running time error。