我有一个字符串, xxxxxxxxxxxxxxxxxxx
我正在将字符串读入较小字符串的结构中,并使用 substr 对其进行解析。我需要将其中一种字符串类型转换为整数。
atoi
不适合我。我该怎么做?它说:
无法将 std::string 转换为 const char*
代码
#include <iostream>
#include <string>
using namespace std;
void main();
{
string s = "453"
int y = atoi(S);
}
原文由 user3472947 发布,翻译遵循 CC BY-SA 4.0 许可协议
std::atoi()
需要const char *
才能传入。将其更改为:
或使用
std::stoi()
您可以直接传递string
:您的程序还有其他几个错误。可行的代码可能是这样的: