C++ String Streams: Strings that are Sources and Destinations of Data

String streams provide a way to use stream semantics with strings. In this way, you can have an in-memory stream that represents textual data. For example, in a GUI application you might want to use streams to build up textual data, but instead of outputting the text to the console or a file, you might want to display the result in a GUI element like a message box or an edit control. Another example could be that you want to pass a string stream around to different functions, while retaining the current read position, so that each function can process the next part of the stream. String streams are also useful for parsing text, because streams have built-in tokenizing functionality.

The ostringstream class is used to write data to a string , while the istringstream class is used to read data from a string . They are both defined in the <sstream> header file. Because ostringstream and istringstream inherit the same behavior as ostream and istream, working with them is pleasantly similar.

The main advantage of a string stream over a standard C++ string is that, in addition to data, the object knows where the next read or write operation will take place, also called current position. There may also be performance benefits depending on the particular implementation of string streams. For example, if you need to append a lot of strings together, it might be more efficient to use a string stream, instead of repeatedly calling the += operator on a string object.

Members

The most commonly used methods and operators from this class are:

Usage

stringstream is a derived class from iostream and uses a string buffer that contains a sequence of characters. This sequence of characters can be accessed directly as a string object, using member str().

You can think of a stringstream as a sort of string that you can write to and read from like a file.

You need need to include the corresponding header file: #include <sstream>. Note that former <strstream> is deprecated.

Declare a stringstream just like an fstream, for example:

stringstream ss;

and, like an fstream or cout, you can write to it:

ss << myString; // or
ss << myCstring; //or
ss << myInt; // or float, or double, etc.

Conversely, you can read from it:

ss >> myChar; // or
ss >> myCstring; // or
ss >> myInt;

This is also an easy way to convert strings of digits into ints, floats or doubles.

Lastly, you can get the entire contents of the stringstream as a single C++ string:

string s = ss.str();

stringstream constructors

explicit stringstream(ios_base::openmode which = ios_base::in | ios_base::out); // default (1)
explicit stringstream(const string& str, ios_base::openmode which = ios_base::in | ios_base::out); // initialization (2)
stringstream(const stringstream&) = delete; // copy (3)
stringstream(stringstream&& x); // move (4)

Thus, a stringstream temporary can be constructed and used like so:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main () {
  string mystr;
  float price=0;
  int quantity=0;

  cout << "Enter price: ";
  getline (cin,mystr);
  stringstream(mystr) >> price;
  cout << "Enter quantity: ";
  getline (cin,mystr);
  stringstream(mystr) >> quantity;
  cout << "Total price: " << price*quantity << endl;
  return 0;
}

Accessing stringstream Buffer

stringstream ss;
streambuf * pssbuf = ss.rdbuf();

...