DEV Community

Suvasish Das
Suvasish Das

Posted on

Stringstream in c++

A stream in c++ is a flow of data into or out to the program. For example, we can say that std::cin is an input stream whose job is to input some data, and std::cout is an output stream whose job is to output or display data. Stringstream in c++ is a class that allows a string object to be treated like a stream. It can be included using #include<sstream> command.

The following diagram shows that stringstream class is derived from iostream class.
Alt Text

Insertion operation

Insertion operation is used to insert value into a stringstream object.

#include<iostream>
#include<sstream>

using namespace std;
int main(){
     stringstream ss;
     ss >> "Hello world!";
return 0;
}
Enter fullscreen mode Exit fullscreen mode

The example shows how string object can inserted in a stringstream variable ss using >> operator. The >> operator return true if succeed, false otherwise.

There are another method to insert value into a stringstream object. Using str method we can take the data string as argument and assigned the data string to the stringstream variable.

#include<iostream>
#include<sstream>

using namespace std;
int main(){
     stringstream ss;
     ss.str("Hello world!");
return 0;
}
Enter fullscreen mode Exit fullscreen mode

This program does the same job as the above program.

Extraction operation

Extraction operation is used to get data from a stringstream object.

#include<iostream>
#include<sstream>

using namespace std;
int main(){
     stringstream ss.str("Hello world!");
     string str;
     ss >> str;   // str variable get the value of ss
     cout << str;     // this line output 'Hello world!'
return 0;
}
Enter fullscreen mode Exit fullscreen mode

Here in this example, extraction operator >> is use to extract the data from a stringstream object. We use a string obj str to store the value.

#include<iostream>
#include<sstream>

using namespace std;
int main(){
     stringstream ss.str("Hello world!");
     cout << ss.str();    // this line output 'Hello world!'
return 0;
}
Enter fullscreen mode Exit fullscreen mode

Here in this example, str() function is used to get the value stored in stringstream.

Practical use

Using stringstream is useful to get integer type data from a given string. Suppose, we have a string my_string which contains "10,20,30" string. So, how can we extract the integers from the given string. Impossible right?
stringstream can be useful solve this type of problems.

#include<iostream>
#include<sstream>

using namespace std;
int main(){
     string my_string = "10,20,30";     // given string
     stringstream ss.str(my_string);     // passing the my_string object to the str method
     int a,b,c;
     char ch;
     ss >> a >> ch >> b >> ch >> c ;     // now the value of a = 10, b = 20, c = 30, and ch = ','
     // ch variable is used for get the inner commas
     cout "a:"<< a << ", b:" << b << ", c:" << c << endl;
     return 0;
}
Enter fullscreen mode Exit fullscreen mode

The above code out put the following result-

     a:10, b:20, c:30
Enter fullscreen mode Exit fullscreen mode

What next?

Hope you understand the basic concept of using stringstream.
Let's solve some problems. click here

Top comments (0)