How to use C++ to get all the STDIN and parse it?
For example, my input is
2
1 4
3
5 6 7
I want to use C++ to read the STDIN using cin and store the each line in an array. So, it will be an vector/array of array of integers.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since this isn’t tagged as homework, here’s a small example of reading from
stdinusingstd::vectors andstd::stringstreams. I added an extra part at the end also for iterating through thevectors and printing out the values. Give the console anEOF(ctrl + d for *nix, ctrl + z for Windows) to stop it from reading in input.Input/Output:
EDIT: Added a couple more comments to the code. Also note that an empty
vectors ofints can be added tovecLines(from an empty line of input), that’s intentional so that the output is the same as the input.