Zach Zach - 1 year ago 70
C++ Question

How would i read this file into a map c++

I am struggling to figure out how to read this file into a map. I started by reading the file into a vector. And then was trying to take the string value in the vector and inserting that into a map. I am completely lost.

The first value is the key and the ordered pairs are the values. I need to be about to add "infinite number of values"

1 2,1 8,2
2 1,1 3,1
3 2,1 4,1
4 3,1 5,1
5 4,1 6,1
6 5,1 7,1
7 6,1 8,1
8 7,1 1,2

Current reading code:

int main(int argc, char const *argv[]){

map<int, string> map;
vector<string> v;
vector<int> v2;
string line;
string fileName(argv[1]);
ifstream inputFile;;

while(getline(inputFile, line)){

return 0;

Answer Source

You specified that the on each line the initial value is a consecutive key, and the remaining values on each line consist of whitespace-delimited tupled, which each tuple being two values separated by a comma, with the first value being some other identifier, and as such the first value of all tuples on the same line will be unique.

As such, I would suggest the following data structure:

typedef std::vector<std::map<int, int>> map_t;

the vector index would be the initial key. Your vector indexes are 0-based, and in your data file they are 1-based, but this is simple enough to handle on the fly. Either change your data file, or just proceed with the key 0 having no data.

Each value in the vector is a map. The map's key is the "other node" id, and the value is the distance to the other node -- according to your description of what this data means.

This data structure will model this data nicely. Now, as far as the matter of reading and parsing this data:

Your initial approach is correct: use std::getline() to read each line at a time into a string:

map_t my_map;

std::string line;

while (std::getline(inputFile, line)) {

    // Here be dragons


There's nothing to be gained from placing each line into a vector of strings, first. We can simply parse each line as we read it. Inside the loop, we'll handle each line read, and place its data directly into my_map. That's the plan. Simple, easy, and straightforward.

Inside the loop, we begin by placing each line into a std::istringstream, in order to parse it, and then extract the initial node id:

int node_id;

std::istringstream line_stream(line);

line_stream >> node_id;

Simple enough. If you need to handle bad input, you should be able to figure out how to check for conversion failures yourself here, and in the remaining code.

Now, we then simply need to pluck out each "other_node,distance" tuple pair:

std::string node_distance;
std::map<int, int> node_distance_map;

while (line_stream >> node_distance)
     // Here be more dragons.

And that's that. Inside the loop, where there are more dragons, node_distance will be each individual "n,n" string, with two values. That's simple enough for you to figure out, on your own: how to extract these two ints out of this string yourself; then update node_distance_map. Then, after this while_loop you take the constructed node_distance_map, and the node_id extract earlier, and stuff the whole thing into the my_map. Mission accomplished. That didn't seem to be very difficult, did it?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download