温馨提示×

c++数字转字符串如何实现

c++
小亿
83
2024-03-20 16:52:45
栏目: 编程语言

在C++中,可以使用标准库中的std::to_string()函数来将数字转换为字符串。示例如下:

#include <iostream>
#include <string>

int main() {
    int num = 12345;
    std::string str = std::to_string(num);
    
    std::cout << "String representation of the number is: " << str << std::endl;
    
    return 0;
}

以上代码将整数12345转换为字符串,并输出结果。您也可以使用std::to_string()函数来将其他类型的数字(如doublefloat等)转换为字符串。

0