If your reading is single-threaded, you can use getchar_unlocked() , which avoids the overhead of blocking the thread for each operation. Follow the symbol, copy the number and wait for the space. When you see a space, save the number and reset the current value:
int *data = new int[MAX_LEN]; int *ptr = data; int ch; *ptr = 0; while ((ch = getchar_unlocked()) != '\n') { if (ch == ' ') { ptr++; *ptr = 0; } else { *ptr = (*ptr *10) + (ch -'0'); } }
Of course, this snippet ignores "non-essential" things, such as error checking, but it is normal when your input is "sanitized". For example, something like this can be used to save I / O costs with the ACM online judge.
source share