Table of Contents

<sstream>

<sstream> provides std::istringstream, std::ostringstream, and std::stringstream for in-memory string I/O. Use them to parse or format strings with the iostream interface.

They're slower than direct string manipulation or std::format, but are useful for portable formatted I/O without file overhead.

Example

This example uses istringstream to parse a formatted input string and ostringstream to compose a formatted output string.

// compile: g++ -std=c++11 -o sstreamexample sstreamexample.cpp
// run: ./sstreamexample
// description: parse and format strings
 
#include <sstream>
#include <iostream>
#include <string>
 
int main() {
    std::istringstream iss("42 3.14 hello");
    int i;
    double d;
    std::string s;
    iss >> i >> d >> s;
    std::cout << "parsed: " << i << ", " << d << ", " << s << "\n";
 
    std::ostringstream oss;
    oss << "result: " << (i + 100);
    std::cout << oss.str() << "\n";
 
    return 0;
}