c++ - 字符串到整数

标签 c++ string

我不能用 C++ 做这个

string temp = "123";
int t = atoi(temp);

为什么????

最佳答案

那是因为 atoi 需要一个原始的 const char* 指针。由于没有从 std::stringconst char* 的隐式转换,您会收到编译器错误。使用 std::stringc_str() 方法为 std::string 对象获取 c 风格的 const char*。顺便说一句,在 C++ 中,您可以使用流来执行此转换,而不是使用这些 C 风格的 API。

关于c++ - 字符串到整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1817992/

相关文章:

C# CS0029 无法将类型 'char' 隐式转换为 'string'

javascript - 在 jquery 中替换字符串中多次出现的多个子字符串

c++ - 使用 libxml2 pretty-print ?

c++ - boost.parameter 和构造函数

c++ - 如何同时接受 std::vector 和 std::initializer_list

javascript - 仅显示一次字符串中的唯一字符

sql-server - 区分大小写的字符串比较

java - 将日历转换为字符串

c++ - 删除单向链表中的 m 个节点

c++ - 如何获得 vector 循环的整数位置