在字符串C++中转换Long?

Set*_*ari 1 c++ string

可能重复:
C++ long to string在C++中将int转换为字符串的
最简单方法

我习惯使用Java,.toString()几乎可以用于任何东西,但我在C++中尝试了一些问题

我无法弄清楚如何将long价值变成一个string.

Ker*_* SB 9

您可以使用字符串流:

#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)