c++中string转int怎么实现

c++
964
2024/2/28 17:26:54
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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打印出来。

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

推荐阅读: c++函数模板使用要注意哪些事项