A stringstream class in C++ is a Stream Class to Operate on strings. The stringstream class Implements the Input/Output Operations on Memory Bases streams i.e. string:
The stringstream class in C++ allows a string object to be treated as a stream. It is used to operate on strings. By treating the strings as streams we can perform extraction and insertion operation from/to string just like cin and cout streams.
These types of operations are mostly useful to convert string to numerical data types and vice versa. The stringstream class also proves to be helpful in different types of parsing.
=> Read Through The Easy C++ Training Series.
Table of Contents:
stringstream Class In C++
A stringstream class can be represented pictorially as follows:
We can see where the stringstream class comes into the picture in the ios diagram. This class is derived from the iostream class. Objects of the stringstream class use a string buffer containing a sequence of characters. This buffer can be accessed directly as a string object.
We can use the str member of the stringstream for this purpose. To use stringstream class in the C++ program, we have to use the header <sstream>.
For Example, the code to extract an integer from the string would be:
string mystr(“2019”); int myInt; stringstream (mystr)>>myInt;
Here we declare a string object with value “2019” and an int object “myInt”. Next, we use the stringstream class constructor to construct a stringstream object from the string object. Then using the extraction operator (>>), the value is extracted into myInt. From the above code, the value of myInt will be 2019.
Let’s explore the various operations of the stringstream class.
Insertion And Extraction Operations Using stringstream
Now we will see how to get data into the stringstream or the insertion operation and how to get data out of the stringstream i.e. the extraction operation of the stringstream class.
#1) Insertion Operation
In order to get the data into a stringstream, we can use two methods.
(i) Using Insertion Operator (<<)
Given a stringstream object ss, we can assign data to the ss buffer as follows using the << operator.
stringstream ss; ss<< “hello,world!!”;
This inserts “hello,world!!” into the stringstream ss.
(ii) Using str(string) Function
We can also use the str function for assigning data to the stringstream buffer. The str function takes the data string as an argument and assigns this data to the stringstream object.
stringstream ss; ss.str(“Hello,World!!”);
#2) Extraction Operation
We have two methods to get the data out of stringstream or for the extraction operation.
(i) Using str() Function
We can use the str() function to get the data out of stringstream as follows.
stringstream ss; ss<<”Hello,World”; cout<<ss.str();
(ii) Using Extraction Operator (>>)
We can use the extraction operator to display the stringstream data as follows.
Stringstream ss; ss<< “stringstream”; string str; ss>>str;
As per the above code, the variable str will have the value of the ss object as a result of the extraction operator action.
Given below is a complete program that demonstrates the usage of Insertion and Extraction operations of the stringstream class.
#include <iostream> #include <sstream> #include<string> using namespace std; int main() { //insertion operator << stringstream os; os << "software "; cout<<os.str(); //str(string) function to read input os.str("TestingHelp"); //str() function for extraction cout<<os.str()<<endl; //extraction operator (>>) stringstream ss; ss<<"26 08 2019"; string mystr1; ss >> mystr1; string mystr2; ss>>mystr2; string mystr3; ss>>mystr3; cout<<mystr1<< "/"<<mystr2<<"/"<<mystr3; }
Output:
In the above program, we have shown the insertion methods first i.e. operator << and str(string) function that reads the string into stringstream.
Next, we saw the working of extraction methods which are str () function that gets the data out of the stringstream and operator >>.
Note that for operator >>, as the initial stringstream data consists of whitespaces while assigning the data to a string variable, it will read only till the first whitespace. Hence to convert the entire stringstream object into string data, we need one variable each to read the data separated by whitespace.
Hence in the above program, we need three string variables to get the entire stringstream object data.
Applications Of stringstream in C++
We can find the uses of stringstream class in various applications.
Some of the applications have been discussed below for your reference:
#1) Conversion Between Strings And Numbers
Insertion and extraction operators of the stringstream work with all basic types of data. Hence we can use them to convert strings to numeric types and vice versa.
The complete program for conversion between strings and numbers is given below.
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { //Numeric to string stringstream ss; int nInt = 2019; double nDouble = 3.142; ss << nInt << " " << nDouble; string myStr1, myStr2; ss >> myStr1 >> myStr2; cout<<"The numeric values converted to string:"<<endl; cout << "myStr1 = "<<myStr1 << " " << "myStr2 = "<<myStr2 << endl; //string to numeric stringstream ns; ns << "2019 3.142"; // insert a string of numbers into the stream int nIntval; double nDoubleval; ns >> nIntval >> nDoubleval; cout<<"The string values converted to numeric types:"<<endl; cout << "nIntval = "<<nIntval << " " <<"nDoubleval = "<< nDoubleval << endl; }
Output:
First, we have converted numeric values into string values. Next, we convert numeric string values into numeric values.
#2) Counting The Number Of Words In A String
We can use the stringstream class to count the number of words in a string. The complete program is given below.
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string str = "Simple Questions To Check Your Software Testing Basic Knowledge"; stringstream s(str); string word; int count = 0; while (s >> word) count++; cout << " Number of words in given string are: " << count; return 0; }
Output:
Number of words in given string are: 9
To count the number of words in a given string, we first convert it to the stringstream object. Then we count each word using an extraction operator (as it stops at each whitespace) in a loop. Finally, we print the value of the total number of words.
#3) Print Individual Word Frequencies In A String
The next application of stringstream in C++ is to print the frequencies of different words in a given string. This means that we will print, how many times a particular word appears in the given string.
For this, we have maintained a map structure that will have a key-value pair with each word in the string as a key and its corresponding value is the frequency of that particular word.
The complete C++ program is shown below.
#include <iostream> #include <sstream> #include <string> #include <map> using namespace std; int main() { string mystr = "Simple Questions To Check Your Software Testing Knowledge "; map<string, int> myMap; stringstream ss(mystr); string Word; while (ss >> Word) myMap[Word]++; map<string, int>::iterator it; for (it = myMap.begin(); it != myMap.end(); it++) cout << it->first << " -> " << it->second << "\n"; return 0; }
Output:
In this program, each word in the string is entered into the map and then the count or frequency of each word is recorded as a value for the corresponding key in the map. This way we output all the words of the string and their corresponding frequencies.
Conclusion
Stringstream class is used for insertion and extraction of data to/from the string objects. It acts as a stream for the string object. The stringstream class is similar to cin and cout streams except that it doesn’t have an input-output channel.
We have discussed various operations of the stringstream class along with several examples of its applications in programming.
In our subsequent tutorials, we will discuss the library functions of the C++ language in detail.
=> Look For The Entire C++ Training Series Here.