C ++ input and output summary

Source: Internet
Author: User

* String class: 1.cin>> string, stop when '\ n' or space is encountered, and' \ n' or space remains in the input, that is, read-only a word or nothing, but the string class handles empty characters or something. the next read will jump.

Line breaks and spaces;

2. getline (cin, string) can be used to read a row into a string. This function continues to read until '\ n' is encountered. Note that getline () is not a class method of cin, cin indicates here to find the input. and this function reads '\ n' and discards it.

Not in string.

 

* Char arr []: 1.cin>> arr is always read, which is not safe. when '\ n' or space is encountered, it is stopped and' \ 0' is automatically added to the backend, but '\ n' or space remains in the input. the next read will also skip line breaks and spaces;

2. Read a row with cin. getline (arr, length) and add '\ 0' to the end to specify the length. read' \ n' and discard it.

 

* Char: 1.cin>> char skips '\ n' or spaces until it does not.

2. cin. get (char) can read every character, including line breaks and spaces. char = cin. get.

 

* Read a row from a file to string: note that the string header file must be included. This is easy to error. getline (ifstream, string); getline is just a function here.

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.