扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

c++中string转int的方法是什么

扬州沐宇科技
2024-01-09 05:08:10
c++

在C++中,可以使用std::stoi()函数将字符串转换为整数。它的使用方法如下:

#include <iostream>
#include <string>

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

    return 0;
}

这将输出:

12345

std::stoi()函数还提供了一些额外的参数,比如指定基数(如二进制、八进制、十进制、十六进制等)、处理前导空白字符和检测转换成功与否等。可以参考C++文档以了解更多关于std::stoi()函数的详细信息。

扫码添加客服微信