Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ std::stringstream class

Reference for std::stringstream, istringstream and ostringstream: build text with <<, parse text with >>, str(), clearing and reuse, and when to use format instead.

The string streams (header <sstream>) are iostreams whose characters live in a std::string instead of a file or the console. std::ostringstream is for building text with <<, std::istringstream is for parsing text with >> and getline, and std::stringstream does both.

They are handy for converting between text and numbers with the same formatting flags used for cout, splitting a line into fields, and capturing the output of code that writes to a std::ostream.

C++
#include <sstream>

std::ostringstream out;
out << "x=" << 42;
std::string s = out.str();   // "x=42"

Try it

C++ Building and parsing text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Splitting words and reusing a stream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
std::istringstream iss(std::string str, std::ios_base::openmode mode = std::ios_base::in);
std::ostringstream oss(std::string str, std::ios_base::openmode mode = std::ios_base::out);
std::stringstream  ss (std::string str, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);

std::string str() const;               // copy of the contents (rvalue overload moves, C++20)
void        str(const std::string& s); // replace the contents
std::string_view view() const;         // C++20: no copy
Name Description
str (constructor) Initial contents of the stream. Optional.
mode Open mode flags. std::ios::ate starts writing at the end; std::ios::app always appends.
s New contents given to str(s). Resets the read and write positions.

Returns: str() returns a std::string copy of everything in the buffer. view() returns a std::string_view of it (C++20), valid until the next change to the stream.

Errors / undefined behavior: failed parses set failbit and do not throw unless exceptions are enabled. str(s) does not clear error flags. Using the result of view() after more output is written is a dangling-view bug.

Usage notes

  • Clear and reset. To reuse a stream, call clear() and str("") (or str(newText)). Forgetting clear() after a failed or EOF read is the most common bug: the stream stays failed.
  • Streaming whitespace. >> splits on whitespace, which makes istringstream a one-line tokenizer; use getline(ss, field, ',') for other separators. See std::getline.
  • Copies. str() copies the whole buffer each time. Call it once and keep the string. Since C++20, std::move(oss).str() moves instead.
  • Stream objects are heavy. Constructing a string stream also builds a locale and buffer, which costs more than std::to_string, std::from_chars or std::format. Use string streams for convenience, not in hot loops.
  • Not for locale-free parsing. A string stream uses its imbued locale. In the default "C" locale numbers use . as the decimal point.
  • Move-only. String streams can be moved but not copied.
  • std::ostringstream with std::cout. Both are std::ostream, so a function taking std::ostream& can print to either.
  • Alternatives. For safe, exception-free number parsing prefer std::from_chars; for formatting prefer std::format.