Using ifstream as fscanf

后端 未结 2 2114
萌比男神i
萌比男神i 2021-02-20 16:56

Assume that I have an input as follows:

N (X_1,Y_1) (X_2,Y_2) .... (X_N, Y_N)

where N, X_i and Y_i are integers.

An example:

         


        
2条回答
  •  一个人的身影
    2021-02-20 17:25

    cin >> N;
    for (int i = 0; i < N; i++)
    {
        cin.ignore(100,'(');
        cin >> X[i];
        cin.ignore(100,',');
        cin >> Y[i];
        cin.ignore(100,')');
    }
    

    It can handle whitespaces also, as it can read input like:

    2  (  1  ,  3  )    (  5  ,  6  )
    

    Demonstration at ideone: http://www.ideone.com/hO0xG

提交回复
热议问题