Loop over map with a map key
how can I loop and print everything from a map like:
map<map<string, int>, map<string, std::vector&开发者_Python百科lt;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());
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.
精彩评论