温馨提示×

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

c++
小亿
86
2024-01-09 05:08:10
栏目: 编程语言

在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()函数的详细信息。

0