+ 2
Adjacency List - input from txt
Hey! I need to write a public transport route planner using adjacency list (smth like this https://code.sololearn.com/cYrF2u2MpPPH/?ref=app) and dijkstra algorithm, but I have troubles with reading input from txt-file, cause it looks like this: Line: "Station (vertex)" interval btw stations in min (weight) "Station" interval "Station" For example: Line 1: "First station" 3 "Second station" 5 "Third station" Line 2: "First station" 2 "Second station" 4 "Third station" 3 "Fourth station" 3 "Fifth station" 2 "Sixth station" etc. I've got all lines using std::getline, however I have no idea how to cut them properly and how I can use strings as vertexes, so I would appreciate so much if somebody shares their thoughts.
2 ответов
0
Just to give you an idea/ something to start with, here is a way how to parse the input obtained from the file:
https://code.sololearn.com/c8vQ7THzZwRg/?ref=app
I used a stringstream to wrap the lines from std::getline(), which enables an interface similar to std::cin. It would also be possible to operate on the string directly via various string methods, but I feel that would be more complicated to implement.
Sadly, I don't have a direct approach regarding the strings as vertices. I've at least never seen an implementation that uses strings for that. If you don't have too many different stations, you could think about using a (unordered) map to translate from strings to indices:
https://en.cppreference.com/w/cpp/container/unordered_map
However, such an approach obviously doesn't scale well for many stations. Another option would be to think about converting the word to an integer via an algorithm, but it would likely take some work on its own. Maybe someone else has a better idea here.
0
Thank you Shadow, your answer helped me a lot! I used map and it worked pretty good :)