A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin).
stringstream provides different methods:
clear() : to clear the stream
str() : to get and set string object whose content is present in stream.
operator << : add a string to the
stringstream object.
operator >> : read content from
the stringstream object,
You can construct a stringstream by passing the string for which you want to use it.
Then you can use stringstream for reading data exactly the way you would use cin. stringstream class is extremely useful in parsing input.
Applications of stringstream :
1. Extract individual words from a sentence or paragraph.
A sentence or a paragraph generally consists of a large number of words and sometimes we want to work on individual words present in the sentence. In that case we can use stringstream to extract words from a complete sentence.
#include <bits/stdc++.h>;
using namespace std;
int main()
{
string str= "Welcome to coding with art";
stringstream ss(str); // declare stringstream for string str
string word;
vector<string> words;
int wordcount = 0;
while(ss >> word){
words.push_back(word);
wordcount++;
}
cout<<"No of words : "<< wordcount <<"\n";
for(string word : words)
cout<<word<<"\n";
return 0;
}
Output :
No of words : 5
Welcome
to
coding
with
art
2. String to number conversion
stringstream allows us to very easily convert strings of digits into ints,
floats or doubles.
Example : string to int conversion
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s = "2456";
// object from the class stringstream
stringstream ss(s);
int number = 0;
ss >> number;
// Now the variable number holds the value 2456
cout << "Value of number : " << number;
return 0;
}
Output :
Value of number : 2456
3. Extract words from a sentence with custom separator (Tokenizing a string)
Sometimes you may be given a string which contains a different separator
than " "(spaces). In that case it becomes more difficult to tokenize the
string into separate words. But using stringstream with getline function allows us to
do this very easily.
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str="Enjoy-Coding-With-Art";
stringstream ss(str); // declare stringstream for string str
string word;
vector<string> words;
int wordcount = 0;
while(getline(ss,word,'-')){ // use getline instead of cin to provide custom separator
words.push_back(word);
wordcount++;
}
cout<<"No of words : "<< wordcount <<"\n";
for(string word : words)
cout<<word<<" ";
return 0;
}
Output :
No of words : 4
Enjoy Coding With Art
4. Process multiple testcases using getline() and stringstream together.
getline() function is used to read a complete line at once. So in case
of multiple test cases, you can fetch each line input in a string using
getline(), and then fetch individual contents of this input using
stringstream.
This is very useful when solving questions on online judge
or giving contests because generally you are provided many test cases per
problem.
Consider you have input of the form :
First line
tells no of each test cases (t)
Next t lines contain contains one integer
and arbitrary number of words per line
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
string line;
cin>>t;
cin.get(); // required to use getline()
while(t--)
{
getline(cin,line); // fetch one test case line completely
stringstream iss(line); // use stringstream for fetching one integer and multiple words
int c;
string word;
iss>>c;
cout<<"Number : "<<c<<"\n";
while(iss>>word)
cout<<word<<" ";
cout<<"\n";
}
}
Input :
3
2 Line1 Coding started
1 Line2 Playing
Cricket and Football
9 Keep coding and working
Output :
Number : 2
Line1 Coding started
Number : 1
Line2 Playing Cricket and Football
Number : 9
Keep coding and working
This brings us to the end of this article. If you have any doubts please mention in the comments section.
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!
2 comments
Click here for commentsamazing contents..one should check out these
ReplyThank you very much!!
Reply