C++ - string类型转换int类型

C语言转换形式:

...
std::string str;
int i = atoi(str.c_str());
...

其参数只能是字符数组,不能是string类型,还有一类函数strtod

 atof()     将字符串转换为双精度浮点型值
 atoi()     将字符串转换为整型值
 atol()     将字符串转换为长整型值
 strtod()   将字符串转换为双精度浮点型值,并报告不能被转换的所有剩余数字
 strtol()   将字符串转换为长整值,并报告不能被转换的所有剩余数字

C++转换格式(C++11):

...
std::string str;
int i = std::stoi(str);
...

同样, 可以使用 stol(long), stof(float), stod(double) 等.

 

参考链接:

1. https://blog.csdn.net/caroline_wendy/article/details/29390573

2. http://c.biancheng.net/cpp/html/1574.html

posted @ 2020-04-12 23:04  Rogn  阅读(22183)  评论(0编辑  收藏  举报