您可以使用字符串流:
#include <sstream>
#include <string>
long x;
// ...
std::ostringstream ss;
ss << x;
std::string result = ss.str();
Run Code Online (Sandbox Code Playgroud)
或者你可以使用Boost lexical_cast:
#include <boost/lexical_cast.hpp>
std::string s = boost::lexical_cast<std::string>(x);
Run Code Online (Sandbox Code Playgroud)
我认为这种语言的这方面并不像它可能那么优雅,这是一种普遍的看法.
在新的C++ 11中,事情变得容易一些,你可以使用这个std::to_string()函数:
#include <string>
std::string s = std::to_string(x);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14434 次 |
| 最近记录: |