Here is a program that will call an arbitrary function for each key that exists on both cards. It is based on an example implementation of std :: set_intersection .
You can change the lambda to run an equality test vendoror any other test you want.
#include <map>
#include <string>
#include <iostream>
template<typename K, typename V1, typename V2, typename Func>
void map_intersection(std::map<K,V1> const &m1, std::map<K,V2> const &m2, Func f)
{
auto it1 = m1.begin(), it2 = m2.begin();
while (it1 != m1.end() && it2 != m2.end() ) {
if (it1->first < it2->first) {
++it1;
} else {
if (!(it2->first < it1->first)) {
f(it1->second, it2->second);
}
++it2;
}
}
}
int main()
{
std::map<std::string, std::string> map1 = { {"a", "apple"}, {"b", "bug"}, {"c", "car"} };
std::map<std::string, std::string> map2 = { {"b", "boat"}, {"c", "car"} };
map_intersection(map1, map2, [](std::string const &v1, std::string const &v2)
{
std::cout << "Same key: " << v1 << "," << v2 << '\n';
});
}
Conclusion:
Same key: bug,boat
Same key: car,car
source
share