C + + input and output Common format (cin,cout,stringstream)

Source: Internet
Author: User

Input format

1, cin>>a;  

    • The most basic format, suitable for all types. Will filter out invisible characters such as spaces, TAB, enter, etc.

2, cin>>noskipws>>ch[i];  

    • NOSKIPWS flow control is used, and whitespace characters are not filtered

3, Cin.get (ch), or ch = cin.get ();  

    • Receives a character, similar to GetChar ();

4, Cin.getline (s,k);  
    • Receives a row of K characters, you can receive a space
    • Cin.getline () actually has three parameters, Cin.getline (string, receive number, end character);
    • When the third parameter is omitted, the system defaults to ' + ';
   5, Getline (cin,s);
    • Like Cin.getline (), a line of strings is read, and it is worth noting that Cin.getline () belongs to the IStream stream, and Getline () is not the same as the string stream.
output Format 1, cout << A;
    • Basic format, suitable for all types

2, cout << setprecision (k) << A;
    • Set the number of bits, similar to%6d in printf;
    • If you want to set the number of decimal places, use cout << fixed << setprecision (k) << A; Control the number of digits after the decimal point
3, cout << setfill (CH) << SETW (k) << A;
    • Set length and padding characters, such as cout << Setfill (' 0 ') << setw (3) << 1; Output 001.

4, cout << hex << setiosflags (ios::uppercase) << A; or cout << hex << Uppercase < < A;
    • Hex controls the format of the output, which defaults to Dec (decimal), which can be set manually dec,hex,oct.
    • Setiosflags is the control format, Ios::uppercase is the output uppercase hexadecimal format, can also omit Setiosflags direct write uppercase. There are 7 different formats
    • Setiosflags (ios::fixed)---fixed floating-point display
    • Setiosflags (ios::scientific)---index indicates
    • Setiosflags (Ios::left)---left-justified
    • Setiosflags (ios::right)---right-aligned
    • Setiosflags (IOS::SKIPWS)---ignore leading whitespace
    • Setiosflags (ios::uppercase)---16 decimal capital output
    • Setiosflags (ios::lowercase)---16-digit lowercase output
   5, Endl

This is also a very important thing in cout, not just to provide a newline, Endl will also flush the buffer.

The cout buffers the output, so the output is not immediately output to the display but is stored in the buffer.

There are three scenarios where the output will be: Flush the buffer, and close the file when the buffer is full.

And Endl is to refresh the buffer, forcing the output, if you want to observe the situation does not flush buffers can add a line # # Endl "\ n", will endl with newline characters, observe the output situation.

    • PS: It is important to note that only the SETW setting is useful when the secondary output is available, the next cout is invalidated, and all other control formats are permanently active.
StringStream

This thing alone, more important, is included in the Sstream library.

The Istringstream class is used to perform input operations for C + + style streaming.
The Ostringstream class is used to perform a C-style streaming output operation.
The StringStream class can also support C-style streaming input and output operations.

Then the StringStream function is to read characters or strings from a string object.

1     strings ="ABCD";2 StringStream SS (s);3     Charch;4      while(ss>>ch) {5cout << Ch <<" ";6     }7 8     //Run Results9     //A B C D

Another example

1     strings ="Hello World";2 StringStream SS (s);3     stringstr;4      while(ss>>str) {5cout << str <<" ";6     }7 8     //Run Results9     //Hello World

When you need to work with strings on some topics, these topics tend to include multiple characters and spaces in the input line, which can be processed using StringStream for a single character or a single string analysis.

Or that sentence, in fact, nothing is difficult, more knock code, more use of familiar with the =7=

C + + input and output Common format (cin,cout,stringstream)

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.