2023年10月16日,周一下午
目录
要将 std::string 类型的数字转换为 int 类型的数字,可以使用 std::stoi 或者 std::atoi 函数。
使用std::stoi函数(推荐)
stoi就是string to int
#include <iostream>
#include <string>
int main() {
std::string strNumber = "12345";
int number = std::stoi(strNumber);
std::cout << "转换后的整数:" << number << std::endl;
return 0;
}
使用std::atoi函数
atoi就是ASCII to int
#include <iostream>
#include <cstdlib>
int main() {
std::string strNumber = "12345";
int number = std::atoi(strNumber.c_str());
std::cout << "转换后的整数:" << number << std::endl;
return 0;
}