温馨提示×

atoi函数在C++中如何使用

c++
小樊
83
2024-08-23 11:36:28
栏目: 编程语言

在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打印转换后的整数。

0