Explain in detail about String stream

Estudies4you

 Q20. Explain in detail about String stream.

Answer:
String Streams
The figure below shows the hierarchy of string streams.
String Streams
Figure: Hierarchy of String Streams
C++ provide the following classes to perform string streams i.e., I/O operations on string. These classes are declared in the <stream.h> header file. These classes are,
istringstream Class
This class support input operations on the strings. This class inherits all the properties of istream class.
ostringstream Class
This class support output operations on the strings. It inherits all the properties of ostream class.
stringstream Class
This class provides support for both the input and output operations on the strings at the same time. It is derived from the iostream class. So it inherits all the properties of iostream class.
stringbuf Class
This class is used to set the string buffers to read and ‘write operations. That is this class provides virtual functions for I/O operations.

The following steps are required to read strings from stream,
1. First create the input string stream object of istringstream class.
2. Read the string passed as an argument to str() function into istring stream object.
The following steps are required to write strings to streams,
1. First create the output string stream object of ostream class.
2: Write the string from string stream using str() function.

For example, the following reads and writes strings into string stream.
#include<sstream.h>
int a, b;
string s = “44, 90”;
istringstream iss; // Create input string stream
iss.str(s); // Specify string to read
iss>>a>>b; // Read strings.
ostring stream oss; // Declare output string stream
oss<<sqrt (b);
s = oss.str( ); // Get created string from output stream

To Top