C++ sstream ostringstream Class
Example
Build a string from text and numbers:
string name = "Adele";
int age = 45;
ostringstream output;
output << name << " is " << age << " years old";
cout << output.str();
Definition and Usage
An ostringstream is an output stream (a stream you write to) that keeps the result in a string. You write to it with <<, just like with cout.
Use str() to get the result as a string. This is useful when you want to build a string from text and numbers.
The ostringstream class is defined in the <sstream> header file.
Syntax
ostringstream name;
Parameter Values
None.
Technical Details
| Returns: | An ostringstream object. Write to it with <<, and use str() to get the result as a string. |
|---|
Related Pages
Read more about strings in our Strings Tutorial.