温馨提示×

c++字符串转数字如何实现

c++
小亿
82
2024-04-23 17:33:47
栏目: 编程语言

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

#include <iostream>
#include <string>

int main() {
    std::string str = "12345";
    int num = std::stoi(str);
    
    std::cout << "String: " << str << std::endl;
    std::cout << "Number: " << num << std::endl;
    
    return 0;
}

在上面的示例中,我们将字符串"12345"转换为整数,并将结果存储在变量num中。然后我们打印出原始字符串和转换后的数字。

0