You can use an ostringstream object to format data into a string. For instance, you could create a string object and an ouput string stream with the statements:
string outBuffer;
ostringstream outStr(outBuffer);
You can now use the insertion operators to write to outBuffer via outStr:
double number = 2.5;
outStr << "number = "<< (number/2);
As a result of the write to the string stream, outBuffer will contain "number = 1.25". The string outBuffer will automatically expand to accomodate however many characters you write to the stream, so it is a very flexible way of forming strings or complex output messages.
The string parameter to the string stream constructor is a reference in each case, so write operations for the ostringstream and stringstream objects act directly on the string object. There is also a default constructor for each of the string stream classes. When you use these, the string stream object will maintain a string object internally, and you can obtain a copy of this using the str() member. For example,
ostringstream outStr;
double number = 2.5;
outStr<< "number = " << (3*number/2);
string ouput = outStr.str();
After these statements, output will contain the string "number = 3.75".
(Ivor Horton's Beginning C++)