You already know that cin reads from the keyboard and cout writes to the screen. stringstream works the same way — but instead of a keyboard or screen, it reads from and writes to a string in memory.
This makes it incredibly useful for two everyday tasks: converting between types (like turning "42" into the integer 42) and parsing strings that contain multiple values.
Setting Up: The <sstream> Header
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
stringstream ss;
ss << "Hello " << 42 << " world";
cout << ss.str() << endl; // Hello 42 world
return 0;
}
The str() method returns the current contents of the stream as a std::string.
Use Case 1: Building Strings from Mixed Types
Before C++11 had std::to_string, stringstream was the standard way to concatenate numbers into strings:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string name = "Player";
int score = 150;
double time = 3.75;
stringstream ss;
ss << name << " scored " << score << " points in " << time << " seconds";
string message = ss.str();
cout << message << endl;
// Output: Player scored 150 points in 3.75 seconds
return 0;
}
Think of ss like a scratch pad where you can mix strings and numbers freely, then grab the result as a single string.
Use Case 2: Converting String to Number
This is probably the most common use of stringstream for beginners:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string input = "1234";
int number;
stringstream ss(input);
ss >> number;
cout << number + 1 << endl; // 1235
return 0;
}
The >> number extraction works just like cin >> number — it reads formatted data and stores it in the variable.
Note: In modern C++11 code, std::stoi() is simpler for this specific case. But stringstream becomes essential when parsing more complex input.
Use Case 3: Parsing Space-Separated Data
This is where stringstream really shines. Say you have a string like "Alice 25 89.5" and want to split it into name, age, and grade:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string line = "Alice 25 89.5";
stringstream ss(line);
string name;
int age;
double grade;
ss >> name >> age >> grade;
cout << "Name: " << name << endl; // Alice
cout << "Age: " << age << endl; // 25
cout << "Grade: " << grade << endl; // 89.5
return 0;
}
The >> operator automatically skips whitespace and extracts each token into the right type. This is far cleaner than manual string splitting.
Use Case 4: Splitting a String by Words
stringstream plus a loop is the easiest way to split a string into individual words:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
string sentence = "the quick brown fox";
stringstream ss(sentence);
vector<string> words;
string word;
while (ss >> word) {
words.push_back(word);
}
for (const string& w : words) {
cout << w << endl;
}
return 0;
}
Output:
the
quick
brown
fox
Each ss >> word extracts the next whitespace-delimited token and returns false when there’s nothing left.
Resetting a stringstream
If you want to reuse a stringstream, you need to clear both the error flags and the content:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
stringstream ss;
ss << "first";
cout << ss.str() << endl; // first
// Reset properly
ss.str(""); // clear content
ss.clear(); // clear error flags
ss << "second";
cout << ss.str() << endl; // second
return 0;
}
Forgetting ss.clear() is a common bug — if the stream hit end-of-file, subsequent reads will silently fail.
istringstream and ostringstream
For clarity, C++ also provides two read-only/write-only variants:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
// Read-only: parse an existing string
istringstream iss("100 200 300");
int a, b, c;
iss >> a >> b >> c;
cout << a + b + c << endl; // 600
// Write-only: build a string
ostringstream oss;
oss << "Result: " << 42;
cout << oss.str() << endl; // Result: 42
return 0;
}
Use istringstream when you’re only reading, ostringstream when you’re only writing. Both work exactly like stringstream for their respective direction.
Related Articles
- C++ String Handling: Everything You Need to Know
- C++ int to string Conversion: 4 Simple Methods
- C++ String to int Conversion
- C++ User Input with cin
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.