且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C ++中将字符串转换为int

更新时间:2022-11-19 09:06:48

要求const char *传递.

将其更改为:

int y = atoi(s.c_str());

或使用 std::stoi() ,您可以通过直接:

or use std::stoi() which you can pass a string directly:

int y = stoi(s);


您的程序还有其他几个错误.可行的代码可能类似于:


You program has several other errors. Workable code could be something like:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s = "453";
    int y = atoi(s.c_str());
    // int y = stoi(s); // another method
}