Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

Read adjacency list of digraph from a text file in C++

I have a text file which has some number of integers in each line. Example:

1 2 4
3 
0 4

2 3

Here 1st line means that the 1st node is connected to nodes numbered 1, 2 and 5. Blank line means 4th node isn't connected to any node.

This gives me a directed graph. This SO question might have been helpful but it assumes each line to have 2 integers, whereas in this case it can have any number of integers from 0 to N-1(N is no. of nodes).

I just want to know how to read this text file. If I had two integers per line, I could have done infile >> a >> b. How to know that " " or end of line has occured. I am not asking for code that makes directed graph.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The accepted answer from the linked question already shows you how to read line by line, which you need to do for your code and the first part shows a loop how to read all numbers from your line (istringstream). Because you don't have a fixed number of entries, you might want to store them in a std::vector.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...