在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()
函数的详细信息。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c++中int转string的方法是什么