C++ sstream str() Function
Example
Get the content of a stringstream, and then replace it:
stringstream ss;
ss << "Number of cars: " << 4;
cout << ss.str() << "\n";
ss.str("New text");
cout << ss.str();
Definition and Usage
The str() function returns the content of a string stream as a string.
If you give it a string, like str("New text"), it replaces the content of the stream with the new string.
The str function is defined in the <sstream> header file.
Tip: Use str("") to empty a string stream, so you can use it again. If you have read from it, also call clear() to reset it.
Syntax
stream.str()
stream.str(text)
stream is a stringstream, an istringstream or an ostringstream.
Parameter Values
| Parameter | Description |
|---|---|
| text | Optional. A string that replaces the content of the stream. |
Technical Details
| Returns: | Without a parameter: a string with the content of the stream. With a parameter: nothing. |
|---|
Related Pages
Read more about strings in our Strings Tutorial.