Ask your own question, for FREE!
Computer Science 6 Online
OpenStudy (anonymous):

How would I load a .txt file like this: "town1:town2:100; town1:town3:200; town2:town3:400;" into c++ and add to a map? The int is the distance between the towns. The idea is that you would be able to query two towns and it would load the distance from the txt file.

OpenStudy (anonymous):

There is a pattern here, two names and 1 integer followed by a semi colon. I'd read the textfile from line to line and split eachline on semi colons first, then on colons. This then you can add the first town and second town as a key in your map like map.add("town1-town2", int distance). Now you have your map of distances. When someone queries two towns, you search in your map on the key of "<firstname>-<secondname>", which will return the value of that key, the distance between those two towns. I don't know the correct c++ syntax, as I haven't done any c++ programming.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!