Use the stream interface to build or parse
std::strings in memory — a stream whose "device" is a string buffer. Header:<sstream>(<spanstream>for fixed buffers, C++23) · Since: C++98
| Type | Use |
|---|---|
std::ostringstream |
build a string with << |
std::istringstream |
parse a string with >> |
std::stringstream |
both |
#include <sstream>
std::string build() {
std::ostringstream oss;
oss << "x=" << 42 << ", y=" << 3.14;
return oss.str(); // "x=42, y=3.14"
}In modern C++,
std::formatusually does this more clearly and faster:std::format("x={}, y={}", 42, 3.14). Reach forostringstreamwhen you need stream semantics (customoperator<<, manipulator state) across many appends.
#include <sstream>
void parse(const std::string& userInput) {
std::istringstream iss{"10 20 30"};
int a, b, c;
iss >> a >> b >> c; // 10, 20, 30
// split a line into words:
std::istringstream line{"the quick brown fox"};
std::vector<std::string> words{std::istream_iterator<std::string>{line},
std::istream_iterator<std::string>{}};
// parse with the same failure-checking as any stream:
std::istringstream in{userInput};
int value;
if (in >> value) { /* parsed */ }
}int n;
std::istringstream{"123"} >> n; // string → int (pre-C++17 idiom)Today prefer std::from_chars (fast,
locale-independent) or std::stoi for this.
<spanstream> gives the same three shapes — std::ospanstream,
std::ispanstream, std::spanstream — over a std::span<char> you own instead
of a std::string the stream owns. Nothing is allocated, which suits stack
buffers and embedded code. GCC 15 and 16 ship it; Clang 22's libc++ doesn't yet.
#include <spanstream>
void fixed_buffer() {
char buf[16];
std::ospanstream out{std::span{buf}};
out << "x=" << 42;
std::span<char> written = out.span(); // the 4 chars written: "x=42"
out << " and far more than sixteen chars";
out.fail(); // true: the buffer is full, it never grows
out.span().size(); // 16
std::ispanstream in{std::string_view{"10 20 30"}};
int a, b, c;
in >> a >> b >> c; // 10, 20, 30, without copying the text
}std::strstream (and istrstream, ostrstream), the char* streams from
<strstream>, were deprecated in C++98 and removed in C++26
(P2867): use spanstream for a fixed buffer or a
string stream for a growing one. Libraries may keep the header as an
extension: libstdc++ (GCC 15 and 16) still compiles std::ostrstream with
-std=c++26, while libc++ (Clang 22) has dropped it, so don't treat
"it compiles" as "it's standard".
str()returns a copy. Holdingoss.str().c_str()dangles — store thestd::stringfirst. (C++20 added astr() &&move overload to reclaim the buffer.)- Reusing a stream: call
.str("")to reset contents and.clear()to reset state flags — both, or stale data/flags bite you. - It's locale-affected and relatively slow. For hot parsing/formatting paths
use
charconv/format; string streams shine for flexible, stream-style code. >>stops at whitespace. To read a phrase, usestd::getline(iss, s).- A full
ospanstreamsetsfailbitand drops the rest. Check the stream after writing;span()holds only what fit.