Istringstream: string stream, which is similar to string truncation. put, str () in the string stream converts the stream into a string.

Source: Internet
Author: User

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 ");

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.