int num = 123; string numStr = to_string(num); // "123"
double value = 3.14159; string valueStr = to_string(value); // "3.14159" string valueStrPrecision = to_string(value).substr(0, 5); // "3.141"
#includeThe `toString` function is not a part of the C++ standard library, but is commonly used in many third-party libraries such as Boost and Qt.class Person { public: string name; int age; string toString() { stringstream ss; ss << "Name: " << name << ", Age: " << age; return ss.str(); } }; Person p {"John", 30}; cout << p.toString(); // "Name: John, Age: 30"