I need to read a string in a following order:
- Read any amount of numbers separated by spaces, discard all but the last one, saving it to
n - Read a space followed by
ncharacters followed by space, save only the characters - Read two more numbers separated by spaces and save them as well
I thought of using string stream to read the numbers and stop at the string, but I don’t know how to predict a string in string stream and stop reading numbers without “reading” the string as number and killing the string stream.
How predict a string and stop reading numbers before it?
Is there a better way to read this whole pattern?
I use C++11.
Edit:
Example input:
1 2 3 4 6 abc de 7 8
Excepted output:
The string: 'abc de'
Number 1: 7
Number 2: 8
You can do that without using any regex, just by using standard C++ streams functionality. Here is an example using std::cin as input stream, but you can use a string stream if you want to read from a string.