Istringstream: string stream, which is similar to string truncation. put, str () in the string stream converts the stream into a string.
Zookeeper
1. istringstream string stream
# Include
# Include
# Include
Usingnamespacestd;
StructMyStruct
{
Stringstr1, str2, str3;
Doubledb;
Intnum;
Charch;
};
Voidmain ()
{
String mystring ("china google microsoft 12.9 123 ");
MyStructstruct1;
Istringstreaminput (mystring); // create a string scan stream
Input> struct1.str1> struct1.str2> struct1.str3> struct1.db> struct1.num> struct1.ch;
Cout <
Cout <
Cout <
Cout <
Cout <
Cout <
Cin. get ();
}
2. Implement functions similar to string Truncation
# Include
# Include
# Include
Usingnamespacestd;
// Implement functions similar to string Truncation
Voidmain ()
{
Charmystring [50] = "china #123 # ";
For (char * p = mystring; * p! = '\ 0'; p ++)
{
If (* p = '#')
{
* P = '';
}
}
Istringstreaminput (mystring); // create a string scan stream
Stringstr;
Intnum;
Charch;
Input> str> num> ch;
Cout <
Cout <
Cout <
Cin. get ();
}
Running result:
3. Implement functions similar to string Truncation
# Include
# Include
# Include
Usingnamespacestd;
// Implement functions similar to string Truncation
Voidmain ()
{
Ostringstream MYOUT;
Charstr [100] = {0 };
// Ostringstream MYOUT (str, sizeof (str ));
Charstr1 [50] = "a1234567b ";
MYOUT <"a1234b" <"" <123 <"" <234.89 <"" <'H' <"" <
Cout <
// Cout <
Cin. get ();
}
The running result is as follows:
4. put in the string stream
# Define_CRT_SECURE_NO_WARNINGS
# Include
# Include
# Include
# Include
Usingnamespacestd;
Voidmain ()
{
Stringstreammystr; // string for Input
Mystr. put ('x'). put ('y'); // a string of characters.
Mystr <"ZXCV"; // string Input
Cout <
Stringstr = mystr. str (); // defines the string accept Value
Charch; // read one character from the character string
Mystr> ch;
Cout <"\ n ";
Cout. put (ch );
Cout <"\ n ";
Cout <
Std: cin. get ();
System ("pause ");
}
Running result
5. str () converts a stream into a string <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vcd4kcjxwiggfsawdupq = "left"> # define_CRT_SECURE_NO_WARNINGS
# Include
# Include
# Include
# Include
Usingnamespacestd;
Voidmain ()
{
Stringstreammystr; // sprintf Function
Charcmd1 [30] = {0 };
Charcmd2 [30] = {0 };
Cin. getline (cmd1, 30). getline (cmd2, 30); // enter two strings
Mystr <
Stringstr = mystr. str (); // defines the string accept Value
System (str. c_str ());
Charcstr [50] = {0}; // default string
Strcpy (cstr, str. c_str ());
Cout <
For (char * p = cstr; * p! = '\ 0'; p ++)
{
If (* p = '&')
{
* P = '';
}
}
Charnewcmd1 [30] = {0 };
Charnewcmd2 [30] = {0 };
Stringstream newstr (cstr); // functions of sscanf
Newstr> newcmd1> newcmd2;
Cout <
System ("pause ");
}