0

how can I loop and print everything from a map like:

map<map<string, int>, map<string, std::vector<int> > >

I tried like this:

ostringstream man2;
man2 << "$$$ -> man2, [nmarcu]:TO BE DELETED - test if IPSecAlarmsMap fill correct" << endl;
map<map<string, int>, map<string, std::vector<int> > >::iterator;
for(iterAlarmsMap = IPSecAlarmsMap.begin(); iterAlarmsMap != IPSecAlarmsMap.end(); iterAlarmsMap++ ) {
    map<string, int>::iterator;
    for(iterMsgMap = iterAlarmsMap->first.begin(); iterMsgMap != iterAlarmsMap->first.end(); iterMsgMap++ ) {
        man2 << "Message: " << iterMsgMap->first << "tunnelId: " << iterMsgMap->second << endl;
    }
    map<string, std::vector<int> >::iterator;
    for(iterTunnelConn = iterAlarmsMap->second.begin(); iterTunnelConn != iterAlarmsMap->second.end(); iterTunnelConn++ ) {
        man2 << "   Tunnel IP: " << iterTunnelConn->first << endl;
        std::vector<int>::iterator iterConnVec;
        for (iterConnVec = iterTunnelConn->second.begin(); iterConnVec!=iterTunnelConn->second.end(); iterConnVec++) {
            man2 << "      Conn= "<< *iterConnVec << endl;
        }
    }
}
trace(man2.str());
Veger
  • 37,240
  • 11
  • 105
  • 116
  • 1
    What happened when you tried like this? This code will not compile by the way. – hamstergene Sep 01 '11 at 09:20
  • 1
    Can you tell us what the problem is? We're not here to analyze your code for you. – Tony The Lion Sep 01 '11 at 09:44
  • Does your map even exist? How are you comparing key values? You could just plug in the [pretty printer](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers) and it should work out of the box. – Kerrek SB Sep 01 '11 at 10:17

1 Answers1

0

With C++11 for-range-loop, you can write something like this:

map<map<string, int>, map<string, std::vector<int>>> original_map;
for (auto& submap_pair : original_map) {
  for (auto& item_pair : submap_pair.first) {
    print(item_pair.first);  // Print the string
    print(item_pair.second); // Print the int
  }
  for (auto& item_pair : submap_pair.second) {
    print(item_pair.first);  // Print the string
    for (auto& vectItem : item_pair.second) { // Traverse through the vector
      print(vectItem);
    }
  }
}

Don't know what you're doing but if I had this map in my code, I would wrap the submaps (map<string,int>, map<string,vector<int>>) and its operations (e.g: print) into separate classes.

DDMC
  • 396
  • 2
  • 11