user123
user123

Reputation: 5407

Mapping int and string values to Map<Integer, List<String>>

In my program I get this sort of result:

2:one
3:ff
3:rr
6:fg

I want to send this data using send() method in socket so that I can get word occurrence and word at receiving socket.

I think Map<Integer, List<String>> would be better option.

My code snippet:

   for (std::map < int, std::vector < std::string > >::iterator hit = three_highest.begin(); hit != three_highest.end(); ++hit) {
        //std::cout << hit->first << ":";

        for (std::vector < std::string >::iterator vit = (*hit).second.begin(); vit != (*hit).second.end(); vit++) {
            std::cout << hit->first << ":";
            std::cout << *vit << "\n";
}
}

hit->first gives occurrence[int val], *vit gives string.

How can I store it into : Map<Integer, List<String>> for each for iteration?

Upvotes: 0

Views: 1636

Answers (1)

David Brown
David Brown

Reputation: 13526

You just construct the lists and insert them into the map.

std::map<int, std::list<std::string> > map_to_string_list;

for (auto list_it = three_highest.begin(); list_it != three_highest.end(); ++list_it) {
    map_to_string_list[list_it->first] = std::list<std::string>(list_it->second.begin(),
                                                                list_it->second.end());
}

Upvotes: 2

Related Questions