How do I convert a value (a number, for example) to a std::string in cpp?

There are two easy ways to do this: you can use the facilities or the library. In general, you should prefer the library.

The library allows you to convert pretty much anything to a std::string using the following syntax (the example converts a double, but you could substitute pretty much anything that prints using the << operator):

// File: convert.h

class BadConversion : public std::runtime_error {
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }

inline std::string stringify(double x)
std::ostringstream o;
if (!(o << x))
throw BadConversion(“stringify(double)”);
return o.str();

The std::ostringstream object o offers formatting facilities just like those for std::cout. You can use manipulators and format flags to control the formatting of the result, just as you can for other std::cout.

In this example, we insert x into o via the overloaded insertion operator, <