C++ examples for STL:string
Using an ostringstream object.
#include <iostream> #include <sstream> #include <string> int main(int argc, const char* argv[]) { std::ostringstream outputString;/* www . j a v a 2 s . co m*/ std::string string1("Output of several data types "); std::string string2("to an ostringstream object:"); std::string string3("\n double: "); std::string string4("\n int: "); std::string string5("\naddress of int: "); double double1 = 123.4567; int integer = 22; // output strings, double and int to ostringstream outputString outputString << string1 << string2 << string3 << double1 << string4 << integer << string5 << &integer; // call str to obtain string contents of the ostringstream std::cout << "outputString contains:\n" << outputString.str(); // add additional characters and call str to output string outputString << "\nmore characters added"; std::cout << "\n\nafter additional stream insertions,\n" << "outputString contains:\n" << outputString.str() << std::endl; return 0; }