Stringstream in c++
Tags: cpp, programming
Published: 2021-08-19 19:17:07

A [stream](https://www.cs.uic.edu/~jbell/CourseNotes/CPlus/FileIO.html) 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](https://www.cplusplus.com/reference/sstream/stringstream/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bgewbw3fkp4kxsac3j9.png)

## Insertion operation

Insertion operation is used to insert value into a `stringstream` object.
```cpp
#include<iostream>
#include<sstream>

using namespace std;
int main(){
     stringstream ss;
     ss >> "Hello world!";
return 0;
}
```
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.

```cpp
#include<iostream>
#include<sstream>

using namespace std;
int main(){
     stringstream ss;
     ss.str("Hello world!");
return 0;
}
```
This program does the same job as the above program.

## Extraction operation

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

```cpp
#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;
}
```
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.

```cpp
#include<iostream>
#include<sstream>

using namespace std;
int main(){
     stringstream ss.str("Hello world!");
     cout << ss.str();    // this line output 'Hello world!'
return 0;
}
```
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. 

```cpp
#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;
}
```
The above code out put the following result-
```
     a:10, b:20, c:30
```
## What next?
Hope you understand the basic concept of using `stringstream`.
Let's solve some problems. [click here](https://www.hackerrank.com/challenges/c-tutorial-stringstream/problem)