Stringstream is a stream class to operate on strings. It implements input/output operations on memory (string) based streams.
stringstream can be helpful in different type of parsing. The following operators/functions are commonly used here
>>Extracts formatted data.
<<Inserts formatted data.
Its header file is
One common use of this class is to parse comma-separated integers from a string (e.g., “23,4,56”).
int a, b, c;
ss >> a >> ch >> b >> ch >> c; // a = 33, b = 7, c = 65
chis a storage area for the discarded commas.
>> operator returns a value, that is a true value for a conditional. Failure to return a value is false.
getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header.
The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.
While doing so the previously stored value in the string object str will be replaced by the input string if any.
istream& getline(istream& is, string& str, char delim);
Return Value: This function returns the same input stream as is which is accepted as parameter.
Try the following example in the editor below.
Given a string A of comma delimited integers, print each integer in a separate line.
A = 33,7,65