I have 3 maps:
map<string, vector<int> > map1
map<string, vector<int> > map2
map<string, vector<int> > map3
I need to create a third map with all the strings existing in both map1 and map2 with their corresponding vectors. Thing is, even if the strings are same, their vectors can be different and i have to append all the vectors from both the common strings into 1 vector.
This is what I’m trying but I’m kind of lost:
for(map<string, vector<int> >::iterator it1=map1.begin(); it1 != map1.end(); it1++)
{
string name = (*it1).first;
for(map<string, vector<int> >::iterator it2=map2.begin(); it2 != map2.end(); it2++)
{
string name2 = (*it2).first;
if (name == name2)
{
map3[name2] = (*it2).second;
}
}
}
Thanks so much for your help!!
Since the maps are ordered you can do this in linear time. This is one possible way to do the task:
Note that this assumes the map key ordering is the default
less<Key>.