1 / 3
May 2008

What's the best/fastest/most convenient way to do it? Especially with numbers separated by space and when the amount of numbers isn't constant in different cases.

  • created

    May '08
  • last reply

    May '08
  • 2

    replies

  • 226

    views

  • 2

    users

If you know the number of space separated integers in each line then you can use cin or scanf(in a for loop). Both cin and scanf ignore whitespaces.
But remember that scanf is much faster than cin.

Oh thanks, I didn't realize you could do it like this:

    int n;
    scanf("%d", &n);
    int a[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);    
    }

I was expecting scanf to always read a single input line, ie. a string followed by return. This is really neat.

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 14 6d

Want to read more? Browse other topics in C and C++ or view latest topics.