You can either use a string stream:
#include <sstream>
#include <string>
long x;
// ...
std::ostringstream ss;
ss << x;
std::string result = ss.str();
Or you can use Boost's lexical_cast
:
#include <boost/lexical_cast.hpp>
std::string s = boost::lexical_cast<std::string>(x);
I think it's a common opinion that this aspect of the language isn't quite as elegant as it could be.
In the new C++11, things are a bit easier, and you can use the std::to_string()
function:
#include <string>
std::string s = std::to_string(x);