How to input space seperated integers and convert them into an array of integers

I’ve recently gotten into competitive programming and I’ve come across some problems where you have a string of space-separated integers as an input. Sometimes, these strings may have a sentinel elements that marks the end of that particular input string. You may want to consider this problem as an example.

Now, I’m new to competitive programming, and my gut says that I’ll have to convert the 3rd and 4th input lines into two arrays of integers (and exclude ‘-1’ from those arrays’). To accomplish this, my first instinct is take input as a string, and then parse through that string, and for all elements between two consecutive whitespaces, use something like atoi at the appropriate places, get the number and store them into an array. I’ll have to stop whenever I encounter ‘-’, ‘1’ and a whitespace or end-of-line, in that particular order.

I was wondering that if the situation occurs, is there a more efficient way to do that. I’m good with C++, but I’m comfortable in Java as well.

use stringstream

you may see this:

use of stringstream

//