Originally from: http://www.cnblogs.com/lancidie/archive/2010/12/03/1895161.html
C + + introduces three classes of Ostringstream, Istringstream, stringstream that must contain sstream.h header files to use them to create an object. It is convenient for converting between types, and dividing the string by spaces to get each part of the split.
The Istringstream class is used to perform input operations for C + + style streaming.
The Ostringstream class is used to perform output operations for C + + style streaming.
The Strstream class can also support C + +-style streaming input and output operations.
The Istringstream class is derived from IStream and Stringstreambase, and Ostringstream is derived from ostream and Stringstreambase, StringStream is derived from the Iostream class and the stringstreambase.
Their inheritance relationship is as follows:
Istringstream is constructed from a string object, and the Istringstream class reads characters from a string object.
The Istringstream constructor 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 A;
float B;
istr>>a;
cout<<a<<endl;
istr>>b;
cout<<b<<endl;
System ("pause");
}
In the above example, when constructing a string stream, the space becomes the internal boundary of the string parameter, and the input "assignment" operation of a A/b object in the example proves this, and the space of the string becomes the decomposition point of the integer data and the floating-point data. Using the method of demarcation we actually completed the split conversion process of string-to-integer object and floating-point object.
The use of the STR () member function allows the Istringstream object to return a string string, such as the output operation in this example (Cout<<istr.str ();).
Ostringstream is also constructed from a string object, and the Ostringstream class inserts a character into a string.
The 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 at the time of construction, then the increment operation does not increase from the end, but modifies the original data, and the excess portion grows
Ostr.put (' d ');
Ostr.put (' e ');
ostr<< "FG";
String gstr = Ostr.str ();
cout<<gstr;
System ("pause");
}
In the previous example code, we can continuously insert a single character or string into the ostr by using the put () or left-shift operator, and return the full string data after the growth through the STR () function, but it is worth noting that when the object is constructed with string data in it, Then the growth operation will not increase from the end, but modify the original data, beyond the growth of the part.
[BASIC_STRINGBUF::STR:
Sets or gets the text in a string buffer without changing the write position. ]
For StringStream, I don't have to say much, and we already know it's input and output for C + + style strings.
The StringStream constructor 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 A;
ostr>>a;
Cout<<a
System ("pause");
}
In addition, objects of the StringStream class are often used to convert string to various built-in type data.
The sample code is as follows:
#i nclude <iostream>
#i nclude <sstream>
#i nclude <string>
using namespace Std;
int main ()
{
StringStream Sstr;
--------int to string-----------
int a=100;
String str;
sstr<<a;
sstr>>str;
cout<<str<<endl;
--------string to char[]--------
Sstr.clear ();//If you want to implement multiple types of conversions by using the same StringStream object, be aware that you must call the clear () member function after each conversion.
StringStream can be used in C + + to facilitate type conversion, string strings, but note that the same StringStream object is reused to continue emptying, and emptying is easy to think of is the clear method, In StringStream this method is actually emptying the state of the stringstream (such as an error, etc.), the actual emptying of the content requires the use of the. STR ("") method.
String name = "Colinguan";
Char cname[200];
sstr<<name;
sstr>>cname;
cout<<cname;
System ("pause");
}
"Go" C + + StringStream Introduction, using methods and examples