0

I'm currently writing a program to implement Dijkstra's algorithm, and decided on using a map object to hold vertices(map key) and the connected vertices paired with the path distance(map value). The connected vertices are paired with the distances like a coordinate pair. Example text file input follows:

1 2,3 3,2
2 4,4
3 2,1 4,2 5,3
4 5,2 6,1
5 6,2
6 1,9

The first number in each line would be read in as a key, where the coordinate pairs would be read into a vector initialized to hold pairs.

I tried using >> with a dummy for the comma, where a and b are the coordinates like:

for(myfile >> key >> a >> dummy >> b){
    //assignment to map here
}

But this doesnt seem like it'd work because there can be more than one coordinate pair in one line.

How would I go about this? I'm newer to C++ and files are still a bit mind-boggling, any help or a couple of steps in the right direction would be great.

TheBudderBomb
  • 73
  • 1
  • 3
  • 14
  • 1
    You're close, but since you have different numbers of pairs on each line, you will have to read line by line and parse the lines individually. [You can take inspiration from Option 2 of this linked answer](https://stackoverflow.com/a/7868998/4581301). Read a line into the `stringstream` Read out the key, then keep reading pairs out until no more pairs. Repeat for next line. – user4581301 Nov 05 '18 at 19:53
  • That looks very helpful in my situation; the next step would be to adapt it to work down to the end of each line, despite the possibility of varying amounts of coordinates. Thanks! – TheBudderBomb Nov 05 '18 at 19:57

0 Answers0