If I have two maps which are guaranteed to have exactly the same set of keys, how can I efficiently iterate through both maps?
For example, say I have the following maps:
std::map<std::string, int> iMap;
std::map<std::string, std::vector<int> > vMap;
At some point they both end up with exactly the same set of keys. I now need to update all values of vMap based on the corresponding iMap value. The first thing that comes to mind is something this:
typedef map<string, int> map_t;
BOOST_FOREACH(map_t::value_type &p, iMap) {
vMap[p.first].push_back(p.second);
}
However, it seems rather wasteful that we have to lookup each value of vMap[n] considering we’re effectively going through the keys in order. Is there any way we can take advantage of this?
If you’re absolutely sure that the keys are identical, you can iterate over both maps in lockstep: