c语言形式
std::string str;
int i = atoi(str.c_str());
c++形式
std::string str;
int i = std::stoi(str);
同样, 可以使用 stol(long), stof(float), stod(double) 等.
参考: http://en.cppreference.com/w/cpp/string/basic_string/stol
转自http://blog.youkuaiyun.com/caroline_wendy/article/details/29390573

本文介绍了如何在C++中将字符串转换为整数、长整型、浮点数及双精度浮点数的方法。提供了两种实现方式:一种是通过标准库函数如`atoi`、`stol`、`stof`、`stod`等,另一种则是利用`std::stoi`函数进行转换。这些函数简化了不同类型数值间的转换过程。
4246

被折叠的 条评论
为什么被折叠?



