There's no itoa in the standard, but in C++11 you can use the std::to_string functions.
n C++11 you can use std::to_string. If this is not available to you, you can use a std::stringstream:
std::stringstream ss; int x = 23;
ss << x;
std::string str = ss.str();
If this is too verbose, there is boost::lexical_cast. There are some complaints about the performance of lexical_cast and std::stringstream, so watch out if this is important to you.
Another option is to use a Boost.Karma, a sublibrary of Spirit. It comes out ahead in most benchmarks.
Usually, itoa is a bad idea. It is neither part of the C nor C++ standard. You can take some care to identify the platforms that support it and use it conditionally, but why should you, when there are better solutions.
本文介绍了在C++11中使用std::to_string将整数转换为字符串的标准方法,并提供了使用std::stringstream作为备选方案的例子。此外还讨论了boost::lexical_cast和Boost.Karma库作为替代解决方案及其性能考量。

156

被折叠的 条评论
为什么被折叠?



