阅读量:73
itoa 函数是一个非标准的 C++ 函数,主要用于将整数转换为字符串
以下是一个使用 std::to_string 的示例:
#include
#include
int main() {
int number = 42;
std::string str_number = std::to_string(number);
std::cout << "The integer is: "<< number class="hljs-string">"The string representation is: "<< str class="hljs-keyword">return 0;
}
如果你需要在不支持 std::to_string 的编译器上实现类似的功能,可以使用 std::ostringstream:
#include
#include
#include
std::string itoa(int value) {
std::ostringstream oss;
oss<< value class="hljs-keyword">return oss.str();
}
int main() {
int number = 42;
std::string str_number = itoa(number);
std::cout << "The integer is: "<< number class="hljs-string">"The string representation is: "<< str class="hljs-keyword">return 0;
}
这两种方法都可以在大多数现代 C++ 编译器上实现跨平台兼容。