cppreference.com > C++ String Streams String Stream Constructors Syntax:
      #include <sstream>
      stringstream()
      stringstream( openmode mode )
      stringstream( string s, openmode mode )
      ostringstream()
      ostringstream( openmode mode )
      ostringstream( string s, openmode mode )
      istringstream()
      istringstream( openmode mode )
      istringstream( string s, openmode mode )
The stringstream, ostringstream, and istringstream objects are used for input and output to a string. They behave in a manner similar to fstream, ofstream and ifstream objects. The optional mode parameter defines how the file is to be opened, according to the io stream mode flags. An ostringstream object can be used to write to a string. This is similar to the C sprintf() function. For example:
      ostringstream s1;
      int i = 22;
      s1 << "Hello " << i << endl;
      string s2 = s1.str();
      cout << s2;
An istringstream object can be used to read from a string. This is similar to the C sscanf() function. For example:
      istringstream stream1;
      string string1 = "25";
      stream1.str(string1);
      int i;
      stream1 >> i;
      cout << i << endl;
You can also specify the input string in the istringstream constructor as in this example:
      string string1 = "25";
      istringstream stream1(string1);
      int i;
      stream1 >> i;
      cout << i;
A stringstream object can be used for both input and output to a string like an fstream object. Related topics: C++ I/O Streams

String Stream Operators
Syntax:

      #include <sstream>
      operator<<
      operator>>

Like C++ I/O Streams, the simplest way to use string streams is to take advantage of the overloaded << and >> operators.

The << operator inserts data into the stream. For example:

      stream1 << "hello" << i;

This example inserts the string "hello" and the variable i into stream1. In contrast, the >> operator extracts data out of a string stream:

      stream1 >> i;

This code reads a value from stream1 and assigns the variable i that value.

Related topics:
C++ I/O Streams

rdbuf
Syntax:

      #include <sstream>
      stringbuf* rdbuf();

The rdbuf() function returns a pointer to the string buffer for the current string stream.

Related topics:
str()
C++ I/O Streams

str
Syntax:

      #include <sstream>
      void str( string s );
      string str();

The function str() can be used in two ways. First, it can be used to get a copy of the string that is being manipulated by the current stream string. This is most useful with output strings. For example:

      ostringstream stream1;
      stream1 << "Testing!" << endl;
      cout << stream1.str();

Second, str() can be used to copy a string into the stream. This is most useful with input strings. For example:

      istringstream stream1;
      string string1 = "25";
      stream1.str(string1);

str() is also handy when you need to clear the stream so that it can be reused:

      ostringstream stream1;
      stream1 << "Using this once" << endl;
      cout << stream1.str();
      stream1.str("");  // Clear the stream
      stream1 << "Now, using it twice" << endl;
      cout << stream1.str();

Related topics:
rdbuf()
C++ I/O Streams