int x,
how to convert x to std::sting type?
Printable View
int x,
how to convert x to std::sting type?
thats the simplest way I know.Code:#include <sstream>
std::stringstream int_to_string;
int x = 0;
int_to_string << x;
std::cout << int_to_string.str() << std::endl;
#include <sstream>
std::stringstream int_to_string;
int x = 0;
int_to_string << x;
std::string str(int_to_string.str());
Correct?
youre creating a new string from the string returned by the .str(). seems it should work. best way to find out is to try it
Alternatively:Code:template <class var_t>
std::string ToString(var_t data)
{
static std::stringstream ss;
ss << data;
return ss.str();
}
If you have boost. Use lexical_cast. It basically does the same thing under the hood, but it provides a uniform interface for all of these types of conversions.
I'm not sure if that's in std::tr1 or not, but it might be.