How to read data from standard input (2)--C + + language

Source: Internet
Author: User

Note: The blogger continues to update this document until the end of the < to be continued > mark Clear

Enter the basic questions to consider

The algorithm competition topic usually has many test cases, uses the Redirect method to send the data to the program.
Then observe whether the output of the program is consistent with the expected results.
In most cases, these tests are present in the form of a file. This should be noted in the following questions:

    1. The data for each row has multiple items, and the delimiter is not fully determined, such as: one or more spaces.
    2. The number of data on each row may not be known. Continued until the end of the bank.
    3. A total number of lines may not be known, until the end of the file.
    4. The last line may not have a carriage return line break and will encounter EOF (file end tag) directly
    5. There may be different ways to line up under different operating systems.
    6. When a string is provided, it may contain spaces.

Let's cite some of the most common examples.

Several lines, one integer per line

Measured data

35-79

Because C + + 's class library is well-packaged, this can be handled concisely as follows:

#include <iostream>usingnamespace std;int main(){    int n;    while(cin >> n){        cout << n << endl;    }        return0;}

On different platforms, the last line has no carriage return, the delimiter is 1 spaces or a number of spaces are well considered.

< to Be continued >

How to read data from standard input (2)--C + + language

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.