I have two multimaps.i would like to create a new multimap which has the common key-value pairs in the given those two multimaps:
for eg:
#include &l
As an alternative solution to Armen's (which is excellent), here's a way to copy and sort at the same time:
typedef std::multimap map_type;
map_type m, n, result;
m.insert(std::make_pair("1-2", "1-1"));
// --------8<--------
n.insert(std::make_pair("1-3", "21-2"));
// --------8<--------
std::set s1(m.begin(), m.end());
std::set s2(n.begin(), n.end());
std::set_intersection(s1.begin(), s1.end(),
s2.begin(), s2.end(),
std::inserter(result, result.end()));
intersection
============
1-2 1-1
1-2 1-2
1-3 2-1
1-3 21-2
To get the elements that are only in m:
result.clear();
std::set_difference(s1.begin(), s1.end(),
s2.begin(), s2.end(),
std::inserter(result, result.end()));
And only in n:
result.clear();
std::set_difference(s2.begin(), s2.end(),
s1.begin(), s1.end(),
std::inserter(result, result.end()));
See it run.
Since you've copied m and n (into s1 and s2) at the time of doing the set_difference(), you could clear() these and insert into them instead of result.