扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

c++中string转int怎么实现

扬州沐宇科技
2024-03-15 17:26:54
c++

在C++中,可以使用std::stoi函数将字符串转换为整数。示例如下:

#include <iostream>
#include <string>

int main() {
    std::string str = "123";
    int num = std::stoi(str);
    
    std::cout << num << std::endl;

    return 0;
}

在上面的示例中,std::stoi函数将字符串"123"转换为整数123,并将其存储在变量num中。然后可以将num打印出来。

扫码添加客服微信