Today, I looked at C ++ cookbook and saw a very useful thing, stringstream, which can easily complete connection string, hexadecimal conversion, format control, and other work. It is located in sstream. h.
# Include <iostream>
# Include <iomanip>
# Include <string>
# Include <sstream>
Using NamespaceSTD;
IntMain ()
{
Stringstream SS;
SS <"There are"<9 <"Apples in my cart .";
Cout <ss. STR () <Endl;
SS. STR ("");
SS <showbase
Cout <ss. STR () <Endl;
SS. STR ("");
SS <setprecision (4) <3.1415927;
Cout <ss. STR () <Endl;
Cin> SS. STR ();
}
Use initialization:
Stringstream. STR ("..")
The other methods are almost the same as the input and output streams. The control words in iomanip can be used to output different formats. The corresponding strings can be obtained using ss. STR.
For example
SS <showbase
Indicates
1) Output hexadecimal notation (0x here)
2) Output hex)
Another example:
DoubleScitodub (ConstString & Str)
{
Stringstream SS (STR );
DoubleD = 0;
SS> D;
If(Ss. Fail ())
{
// Add code to solve the exception
}
ReturnD;
}
The stringstream> double method can be called to convert the string represented by the scientific notation to the double type, and check whether the conversion is successful by stringstream. Fail ().