C ++ introduces three classes: ostringstream, istringstream, and stringstream. To use these classes to create objects, the sstream. h header file must be included.
The istringstream class is used to perform input operations for C ++-style streaming.
The ostringstream class is used to perform C-style streaming output operations.
The strstream class also supports the input and output operations of C-style streaming.
The istringstream class is derived from istream and stringstreambase. ostringstream is derived from ostream and stringstreambase, while stringstream is derived from the iostream class and stringstreambase.
Shows their inheritance relationships:
Istringstream is constructed from a string object. The istringstream class reads characters from a string object.
Istringstream constructor prototype is as follows:
Istringstream: istringstream (string Str );
# I nclude <iostream>
# I nclude <sstream>
Using namespace STD;
Int main ()
{
Istringstream istr;
Istr. STR ("1 56.7 ",);
// The above two processes can be simply written as istringstream istr ("1 56.7 ");
Cout <istr. STR () <Endl;
Int;
Float B;
Istr>;
Cout <A <Endl;
Istr> B;
Cout <B <Endl;
System ("pause ");
}
In the preceding example, when constructing a string stream, spaces are the internal demarcation between string parameters. In this example, the input "value assignment" Operation of object A and object B demonstrates this, the space in the string is the decomposition point of the integer data and floating point data. The division and acquisition method are used to complete the splitting and conversion process from the string to the integer object and floating point object.
The use of the STR () member function allows the istringstream object to return a string (for example, the output operation in this example (cout <istr. STR ();).
Ostringstream is also constructed by a string object. The ostringstream class inserts characters into a string.
The prototype of ostringstream constructor is as follows:
Ostringstream: ostringstream (string Str );
The sample code is as follows:
# I nclude <iostream>
# I nclude <sstream>
# I nclude <string>
Using namespace STD;
Int main ()
{
Ostringstream ostr;
// Ostr. STR ("ABC"); // If a string parameter is set during the construction, the original data is modified instead of the end of the growth operation.
Ostr. Put ('D ');
Ostr. Put ('E ');
Ostr <"FG ";
String gstr = ostr. STR ();
Cout <gstr;
System ("pause ");
}
In the above code, we can use the put () or left shift operator to continuously insert a single character or string to ostr, and use the STR () function to return the complete string data after growth, note that when constructing a string data in an object, the growth operation does not increase from the end, but changes the original data, the excess is partially increased.
[Basic_stringbuf: Str:
Sets or gets the text in a string buffer without changing the write position.]
For stringstream, I don't need to talk about it. As you know, it is used for input and output of C ++-style strings.
Stringstream constructor prototype is as follows:
Stringstream: stringstream (string Str );
The sample code is as follows:
# I nclude <iostream>
# I nclude <sstream>
# I nclude <string>
Using namespace STD;
Int main ()
{
Stringstream ostr ("CCC ");
Ostr. Put ('D ');
Ostr. Put ('E ');
Ostr <"FG ";
String gstr = ostr. STR ();
Cout <gstr <Endl;
Char;
Ostr>;
Cout <
System ("pause ");
}
In addition, the stringstream class object is also commonly used for conversion between string and various built-in data types.
The sample code is as follows:
# I nclude <iostream>
# I nclude <sstream>
# I nclude <string>
Using namespace STD;
Int main ()
{
Stringstream SSTR;
// -------- Convert int to string -----------
Int A = 100;
String STR;
SSTR <;
SSTR> STR;
Cout <STR <Endl;
// -------- Convert string to Char [] --------
SSTR. Clear (); // if you want to convert multiple types by using the same stringstream object, you must call the clear () member function after each conversion.
String name = "colinguan ";
Char cname [200];
SSTR <name;
SSTR> cname;
Cout <cname;
System ("pause ");
}