在C++中,可以使用std::atoi函数来将字符串转换为整数。以下是使用std::atoi函数的示例:
#include <iostream>
#include <cstdlib>
int main() {
const char* str = "12345";
int num = std::atoi(str);
std::cout << "Converted string to integer: " << num << std::endl;
return 0;
}
在上面的示例中,我们首先包含了<cstdlib>头文件,然后定义了一个字符串str,然后使用std::atoi函数将字符串转换为整数并将结果存储在num变量中。最后,我们使用std::cout打印转换后的整数。