printing the vector in a map

12,986

Solution 1

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 << " ";

Solution 2

I know this question is a bit old, but I had a similar question and this post helped me out, so I guess I can post my solution here. Based upon example found here: map and multimap I have a map with a pair <string, vector<string> > where vector<string> will, of course, contain more than one value

#include <string.h>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

using namespace std;

int main() {
   map< string, vector<string> > Employees;
   vector <string> myVec;
   string val1, val2, val3;
   val1 = "valor1";
   val2 = "valor2";
   val3 = "valor3";

   // Examples of assigning Map container contents
   // 1) Assignment using array index notation
   Employees["Mike C."] = {"val1","val2", "val3"};
   Employees["Charlie M."] = {"val1","val2", "val3"};

   // 2) Assignment using member function insert() and STL pair
   Employees.insert(std::pair<string,vector<string> >("David D.",{val1,val2,val3}));

   // 3) Assignment using member function insert() and "value_type()"
   Employees.insert(map<string,vector<string> >::value_type("John A.",{"val7","val8", "val9"}));

   // 4) Assignment using member function insert() and "make_pair()"
   myVec.push_back("val4");
   myVec.push_back(val1);
   myVec.push_back("val6");
   Employees.insert(std::make_pair("Peter Q.",myVec));

   cout << "Map size: " << Employees.size() << endl;

   for(map<string, vector<string> >::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii){
       cout << (*ii).first << ": ";
       vector <string> inVect = (*ii).second;
       for (unsigned j=0; j<inVect.size(); j++){
           cout << inVect[j] << " ";
       }
       cout << endl;
   }
}

You may notice different ways to add information, as well as the printing part, which prints the pairs "key-vector" where vector has several values. We can also print like this if C++11:

for(auto ii=Employees.begin(); ii!=Employees.end(); ++ii){
   cout << (*ii).first << ": ";
   vector <string> inVect = (*ii).second;
   for (unsigned j=0; j<inVect.size(); j++){
       cout << inVect[j] << " ";
   }
   cout << endl;
}

Output will be as follows:

Map size: 5
Charlie M.: val1 val2 val3 
David D.: valor1 aVal1 valor3 
John A.: val7 val8 val9 
Mike C.: val1 val2 val3 
Peter Q.: val4 valor1 val6 

P.S.: I don't know why the output is in different order, I believe the different push methods and their speed have something to do with it.

Solution 3

In C++ 11 you can do:

for(auto mapIt = begin(dict); mapIt != end(dict); ++mapIt)
{
    std::cout << mapIt->first << " : ";

    for(auto c : mapIt->second)
    {
        std::cout << c << " ";
    }

    std::cout << std::endl;
}

Note the non-member begin/end. Also, if you don't need ostream flushing, throw out the std::endl, of course.

Share:
12,986
gandolf
Author by

gandolf

Just your everyday programming enthusiast

Updated on June 04, 2022

Comments

  • gandolf
    gandolf over 1 year

    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.