I want to read graph adjacency information from a text file and store it into a vector.
-
the file has arbitrary number of lines
-
each line has arbitrary number of integers ended with ‘\n’
for example,
First line:
0 1 4
Second line:
1 0 4 3 2
Thrid line:
2 1 3
Fourth line:
3 1 2 4
Fifth line:
4 0 1 3
If I use getline() to read one line at a time, how do I parse the line (as each line has variable number of integers)?
Any suggestions?
The standard line reading idiom:
Here’s a one-line version using a
forloop. We need an auxiliary construction (credits to @Luc Danton!) that does the opposite ofstd::move: