Search code examples
c++dictionarystdstdvectorstdmap

printing the vector in a map


I have a map defined by:

map < char, vector < unsigned char>> dict;

After a function generates and adds the contents to this dictionary, I want to next iterate through and print each key:value pair in a loop.

for(auto it = dict.begin(); it != dict.end(); ++it)
{
    cout << it.first << " : ";
    // how to output the vector here? since the len of value differs
    // for each key I need that size
    for( unsigned int s = it.size()
}

How can I get the size of the value from the iterator so that I can iterate throught he vector to output it.


Solution

  • it.second will give you a copy of the vector for the given map element so you could change your inner loop to

    for(auto it2 = it->second.begin(); it2 != it->second.end(); ++it2)
        cout << *it2 << " ";