温馨提示×

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

c++
小亿
95
2024-04-23 17:33:47
栏目: 编程语言
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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中。然后我们打印出原始字符串和转换后的数字。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c++字符串转数字怎么实现

0