The std::istringstream is a string class object which is used to stream the string into different variables and similarly files can be stream into strings. Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed as a string object.
1. Streaming integer from a string with std::istringstream
One way to stream a string is to use an input string stream object std::istringstream from the header. Once a std::istringstream object has been created, then the string can be streamed and stored using the extraction operator(>>). The extraction operator will read until whitespace is reached or until the stream fails.
Below is the illustration of the std::istringstream:
The std::istringstream object can also be used as a boolean to determine if the last extraction operation failed. This happens if there wasn’t any more of the string to the stream, For Example, If the stream still has more characters then we are able to stream the string again.
The extraction operator >> writes the stream to the variable on the right of the operator and returns the std::istringstream object, so the entire expression my_stream >> n is a std::istringstream object which returns a boolean i.e., true if stream is possible else return false.
Below is the implementation of using the std::istringstream in the above way:
That stream was successful: 1 That stream was successful: 2 That stream was successful: 3 That stream was NOT successful!
2. Strings with Mixed Types
In the above illustrations, the string contains only whitespaces and characters which could be converted to int. If the string has mixed types i.e., it contains more than one data type in the stream then it can be used as illustrated below.
Below is the illustration of the std::istringstream for the mixed types:
That stream was successful: 1, That stream was successful: 2, The stream has failed.
abc def ghi
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if two strings after processing backspace character are equal or not
- Find the numbers of strings that can be formed after processing Q queries
- String after processing backspace characters
- Meta Strings (Check if two strings can become same after a swap in one string)
- Search in an array of strings where non-empty strings are sorted
- Count of same length Strings that exists lexicographically in between two given Strings
- Pairs of complete strings in two sets of strings
- Number of common base strings for two strings
- Count of strings that become equal to one of the two strings after one removal
- Distinct strings such that they contains given strings as sub-sequences
- Arrays and Strings in C++
- Concatenation of two strings in PHP
- Comparing two strings in C++
- XOR of two Binary Strings
- <strings> library in C++ STL
- Add n binary strings
- Strings in C++ and How to Create them?
- Practice questions on Strings
- Output of C++ programs | Set 29 (Strings)
- Sub-strings of length K containing same character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.