C++ sstream stringstream Class
Example
Write three words to a stringstream, and read them back one by one:
stringstream ss;
string car;
ss << "Volvo BMW Ford";
while (ss >> car) {
cout << car << "\n";
}
Definition and Usage
A stringstream is a stream (a flow of data) that keeps its data in a string. You write to it with << and read from it with >>, just like with cout and cin.
When you read with >>, you get one word at a time, and the spaces between the words are skipped. This is often used to split a string into words.
The stringstream class is defined in the <sstream> header file.
Tip: If you only need to read from a string, use istringstream. If you only need to write to a string, use ostringstream.
Syntax
stringstream name;
stringstream name(text);
Parameter Values
| Parameter | Description |
|---|---|
| text | Optional. A string with the start content of the stream. |
Technical Details
| Returns: | A stringstream object. Write to it with <<, read from it with >>, and use str() to get the content as a string. |
|---|
Related Pages
Read more about strings in our Strings Tutorial.