替代 itoa() 将整数转换为字符串 C++?

2022-01-14 00:00:00 integer c++ stdstring itoa

我想知道是否有 itoa() 的替代方法可以将整数转换为字符串,因为当我在 Visual Studio 中运行它时会收到警告,并且当我尝试在Linux,我得到一个编译错误.

I was wondering if there was an alternative to itoa() for converting an integer to a string because when I run it in visual Studio I get warnings, and when I try to build my program under Linux, I get a compilation error.

推荐答案

在 C++11 中你可以使用 std::to_string:

In C++11 you can use std::to_string:

#include <string>

std::string s = std::to_string(5);

如果您使用的是 C++11 之前的版本,则可以使用 C++ 流:

If you're working with prior to C++11, you could use C++ streams:

#include <sstream>

int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

取自 http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/

相关文章