J
jacek.dziedzic
Hi!
What is the canonical way of finding an intersection of two
std::maps?
i.e. I have
std::map<whatever,size_t> map1;
std::map<whatever,size_t> map2;
.... and I need an std::vector containing all the values that occur
simultaneously in both of the maps, nevermind the keys. In each
of the maps the values do not repeat.
One idea is to copy all values from map1 into a vector, and then
for each value from map2 check if it's present in the vector already,
if not -- then push it into the vector. This, of course, has ugly
complexity, so perhaps a set would do better. Is there a better
way?
I was also thinking of putting the values into a multiset and then
taking out all values with a count of 2, but there must be a simpler
way.
TIA,
- J.
What is the canonical way of finding an intersection of two
std::maps?
i.e. I have
std::map<whatever,size_t> map1;
std::map<whatever,size_t> map2;
.... and I need an std::vector containing all the values that occur
simultaneously in both of the maps, nevermind the keys. In each
of the maps the values do not repeat.
One idea is to copy all values from map1 into a vector, and then
for each value from map2 check if it's present in the vector already,
if not -- then push it into the vector. This, of course, has ugly
complexity, so perhaps a set would do better. Is there a better
way?
I was also thinking of putting the values into a multiset and then
taking out all values with a count of 2, but there must be a simpler
way.
TIA,
- J.