C++ sstream istringstream Class
Example
Read a name and an age from a string:
istringstream input("John 32");
string name;
int age;
input >> name >> age;
cout << name << " is " << age << " years old";
Definition and Usage
An istringstream is an input stream (a stream you read from) that gets its data from a string. You read from it with >>, just like with cin.
The >> operator converts the text to the type of the variable. In the example, the text "32" is stored as the int 32.
The istringstream class is defined in the <sstream> header file.
Tip: See how to count the words in a string in How To Count Words in a String.
Syntax
istringstream name(text);
Parameter Values
| Parameter | Description |
|---|---|
| text | Required. The string to read from. |
Technical Details
| Returns: | An istringstream object. Read from it with >>. |
|---|
Related Pages
Read more about strings in our Strings Tutorial.