C++ String Streams

C++ Reference

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;  // displays 25

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 << endl;  // displays 25

A stringstream object can be used for both input and output to a string like an fstream object.

Related topics:

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:

rdbuf
Syntax:
  #include <sstream>
  stringbuf* rdbuf();

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

Related topics:

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(), along with clear(), is also handy when you need to clear the stream so that it can be reused:

  istringstream stream1;
  float num;

  // use it once
  string string1 = "25 1 3.235\n1111111\n222222";
  stream1.str(string1);
  while( stream1 >> num ) cout << "num: " << num << endl;  // displays numbers, one per line

  // use the same string stream again with clear() and str()
  string string2 = "1 2 3 4 5  6 7 8 9 10";
  stream1.clear();
  stream1.str(string2);

  while( stream1 >> num ) cout << "num: " << num << endl;  // displays numbers, one per line
Related topics: