pls explain the concept of stringstream class in c++.
thnks
Stringstream class
A stringstream associates a string object with a stream
means if you write
stringstream name(string);
now name is a stream(like input stream) you can take input from this stream
stringstream class is extremely useful in parsing input.
pls explain with an example
for ex you want to count no of words in given string
string str;
getline(cin,str);
stringstream ss(str);
`int` `count = 0; `
while (s >> word)
count++;
return count;
Application
conversion of string to num
string str;
cin>>str;
stringstream ss(str);
int num;
ss>>num;
num stores no which is in str
pls explain working of the while loop,
especially working of >>
thanks
similar to cin>>
now as our stream is ss so we are taking input from (ss>>word)
when input is complete it will throw null and ( as null is consider as false statement) so we come out of loop
how it will count individual words?.
thnks
as cin take only one word at a time so when word is taken it increase count
how it will know it has taken one word?
as cin doesn’t take whitespaces so it will stop taking input
you can try it
string str;
cin>>str;
cout<<str;
in input provide " full name"
output will be only full