stringstream in C++ and its Applications

Last Updated : 31 Aug, 2026

Stringstream is a C++ Standard Library class defined in the <sstream> header that allows strings to be treated as input and output streams. It can be used to extract data from strings like cin and build strings by inserting values like cout.

  • It supports formatted input and output operations using a string as the underlying stream.
  • It is commonly used for type conversion, parsing strings, and combining multiple values.

Example: The following example shows how stringstream can be used to extract an integer from a string.

C++
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string str = "123";
    int num;

    stringstream ss(str);
    ss >> num;

    cout << "Integer: " << num << endl;

    return 0;
} 

Output
Integer: 123

Explanation: The string "123" is stored in the stringstream object ss. The >> operator extracts the value from the stream and stores it as an integer in num.

Types of String Streams

C++ provides three string stream classes in the <sstream> header. Each class is designed for a specific type of operation on string data:

  • stringstream: Supports both input and output operations. It can be used to read data from a string as well as insert data into the stream.
  • istringstream: Supports input operations only. It is useful when extracting or parsing values from an existing string.
  • ostringstream: Supports output operations only. It is useful for combining different values and creating a formatted string.

Examples of stringstream

1. Converting String to Integer

A stringstream can convert a numeric string into an integer using the extraction operator (>>).

C++
#include <iostream>   
#include <sstream>    

using namespace std;

int main() {
    string str = "123";   
    int num;              
    // Create a stringstream object initialized with 'str'
    stringstream ss(str);                     
    // Extract an integer from the stringstream and store it in 'num'
    ss >> num;            
    cout << "Integer: " << num << endl;  

    return 0;             
}

Output
Integer: 123

Explanation: The string "123" is inserted into the stream, and >> extracts its value as an integer into num.

2. Converting Integer to String

A stringstream can convert an integer into a string by inserting the value into the stream and retrieving it using str().

C++
#include <iostream>   
#include <sstream>    

using namespace std;

int main() {
    int num = 456;     
    string str;        
    //Create an empty stringstream object
    stringstream ss;  
     // Insert the integer 'num' into the stringstream
     // This converts the number into characters inside the stream
    ss << num;        
    // Extract the contents of the stream as a string and store it in 'str'
    ss>>str;

    cout << "String: " << str << endl;  

    return 0;          
}

Output
String: 456

Explanation: The integer 456 is inserted into the stream using <<. The str() function then returns the stream contents as a string.

3. Splitting a Sentence into Words

A stringstream can extract individual words from a sentence using the >> operator.

C++
#include <iostream>   
#include <sstream>   
#include <string>     

using namespace std;

int main() {
    string sentence = "C++ is powerful";  
    string word;                         
    // Create a stringstream object initialized with the sentence
    // This lets us read word by word like a stream
    stringstream ss(sentence);            

    // Extract words from the stringstream one by one until no more words left
    while (ss >> word) {
        cout << word << endl;             
    }

    return 0;                            
}

Output
C++
is
powerful

Explanation: The sentence is stored in the stringstream, and each >> operation extracts the next word separated by whitespace.

4. Combining Multiple Values into a String

A stringstream can combine text and values of different data types into a single string.

C++
#include <iostream>   
#include <sstream>    

using namespace std;

int main() {
    int age = 25;          
    string name = "John";  
    // Create an empty stringstream object
    stringstream ss;       
    // Insert multiple pieces of data (text, variables) into the stringstream
    ss << "Name: " << name << ", Age: " << age;
    // Get the combined string from the stringstream
    string result = ss.str();

  
    cout << result << endl;

    return 0;              
}

Output
Name: John, Age: 25

Explanation: The << operator inserts the text, name, and age into the stream. The str() function returns the complete content as a string.

5. Clearing and Reusing a stringstream

A stringstream can be reused by clearing its stored content and resetting its stream state.

C++
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    stringstream ss;

    // Put some data into the stringstream
    ss << "Hello, world!";
    cout << "Before clearing: " << ss.str() << endl;

    // Clear the contents of the stringstream
    ss.str("");     

    // Reset the stringstream's state flags (like eof, fail)
    ss.clear();      

    // Now we can reuse the stringstream for new data
    ss << "New data here!";
    cout << "After clearing and reuse: " << ss.str() << endl;

    return 0;
}

Output
Before clearing: Hello, world!
After clearing and reuse: New data here!

Explanation: str("") clears the stored string, while clear() resets the stream state. The stream can then be used again for new data.

Comment