C++ stringstream is a stream class on strings. This allows us to take a string and treat it as if it were a stream object, like cin
, or cout
.
Due to this, we can perform operations like indirection and out-direction, using <<
and >>
.
Let’s look at how we can use this class to parse strings easily.
Create a stringstream object
We can convert a string, and create a stringstream
object.
But before that, we need to include the relevant header file, which we can get at the<sstream>
header file.
#include <sstream>
To create the object, it’s really simple. The relevant type is stringstream
, and we need to input a string into the constructor.
#include <iostream> #include <string> #include <sstream> // We use the std:: namespace, // otherwise we need to specify the // stringstream using std::stringstream using namespace std; int main() { // Input string string input_string = "Hello from JournalDev"; // Create the stringstream from the input string stringstream ss(input_string); cout << "Created a stringstream objectn"; return 0; }
Output
Created a stringstream object
Right now, we’ve not made use of the stringstream object yet. Let’s now look at how we can read or write from the stream.
Read into the C++ stringstream
Similar to cin
, we can do this using the >>
operator, to read a string from the stringstream.
When we initialize the stringstream object using a string, the contents of the string will be treated as a stream-like object.
string word; // Read from the stringstream into word ss >> word; cout << word << endl;
So, whenever we read from the stream, we will get a string! The advantage of this method is that it automatically parses delimiters like spaces, commas, etc.
We can use stringstream
to parse our input string and get an array of strings, after parsing with delimiters!
#include <iostream> #include <string> #include <sstream> // We use the std:: namespace, // otherwise we need to specify the // stringstream using std::stringstream using namespace std; int main() { // Input string string input_string = "Hello from JournalDev"; // Create the stringstream from the input string stringstream ss(input_string); cout << "Created a stringstream objectn"; // To store the words that we get back from the stringstream string words[5]; string buffer; int i = 0; while (ss >> buffer) { words[i] = buffer; cout << "Buffer: " << buffer << endl; i++; } // Print the tokenized set of strings from words[] cout << "Printing the tokenized array of stringsn"; for (int j = 0; j < i; j++) { cout << words[j] << endl; } return 0; }
Here, we check if our stream is empty, using ss >> buffer
. If ss
is not empty, it will write to buffer
and go into the body of the loop. Otherwise, it will simply exit from the loop.
Since our input string contains spaces, the output will be a tokenized list of strings, after removing the spaces.
Output
Created a stringstream object Buffer: Hello Buffer: from Buffer: JournalDev Printing the tokenized array of strings Hello from JournalDev
Write into the stringstream
Similar to reading from the stream, we can also write into the stream, just like cout <<
!
Unlike our previous example, instead of initializing the stringstream
object using the constructor, we can also simply declare it, and then write to it later!
// Input string string input_string = "Hello from JournalDev"; // Create an empty stringstream object stringstream ss; ss << input_string;
So, we will get the same output as before, but we have also written into our stream now!
#include <iostream> #include <string> #include <sstream> // We use the std:: namespace, // otherwise we need to specify the // stringstream using std::stringstream using namespace std; int main() { // Input string string input_string = "Hello from JournalDev"; // Create the stringstream from the input string stringstream ss; cout << "Created a stringstream objectn"; // To store the words that we get back from the stringstream string words[5]; string buffer; ss << input_string; int i = 0; while (ss >> buffer) { words[i] = buffer; cout << "Buffer: " << buffer << endl; i++; } // Print the tokenized set of strings from words[] cout << "Printing the tokenized array of stringsn"; for (int j = 0; j < i; j++) { cout << words[j] << endl; } return 0; }
Output
Created a stringstream object Buffer: Hello Buffer: from Buffer: JournalDev Printing the tokenized array of strings Hello from JournalDev
Clear the C++ stringstream
Suppose you want to erase whatever content is currently on ss
, there is a simple function for that:
ss.clear();
This simply clears whatever content is there in the buffer.
#include <iostream> #include <string> #include <sstream> // We use the std:: namespace, // otherwise we need to specify the // stringstream using std::stringstream using namespace std; int main() { // Input string string input_string = "Hello from JournalDev"; // Create the stringstream from the input string stringstream ss; cout << "Created a stringstream objectn"; // To store the words that we get back from the stringstream string words[5]; string buffer; ss << input_string; int i = 0; while (ss >> buffer) { words[i] = buffer; cout << "Buffer: " << buffer << endl; i++; } // Print the tokenized set of strings from words[] cout << "Printing the tokenized array of stringsn"; for (int j = 0; j < i; j++) { cout << words[j] << endl; } ss.clear(); if (ss >> buffer) { cout << "Stream is not empty: Contains " << buffer << endl; } else { cout << "Stream is empty!n"; } return 0; }
Output
Created a stringstream object Buffer: Hello Buffer: from Buffer: JournalDev Printing the tokenized array of strings Hello from JournalDev Stream is empty!
We check if our stream is actually empty, using if (ss >> buffer)
. Since the check fails, we have indeed cleared our stringstream!
Conclusion
In this article, we learned about how we could use the stringstream object in C++, to easily parse strings!
For similar topics on C++, do visit our C++ tutorials page.