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 <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
multimap<std::string, std::string> m;
multimap<std::string, std::string> n;
m.insert(multimap<string,string>::value_type("1-2","1-1"));
m.insert(multimap<string,string>::value_type("1-2","1-2"));
m.insert(multimap<string,string>::value_type("1-2","1-3"));
m.insert(multimap<string,string>::value_type("1-2","1-4"));
m.insert(multimap<string,string>::value_type("1-3","2-1"));
m.insert(multimap<string,string>::value_type("1-3","21-1"));
m.insert(multimap<string,string>::value_type("1-3","21-2"));
n.insert(multimap<string,string>::value_type("1-2","1-1"));
n.insert(multimap<string,string>::value_type("1-2","1-2"));
n.insert(multimap<string,string>::value_type("1-2","1-5"));
n.insert(multimap<string,string>::value_type("1-2","1-7"));
n.insert(multimap<string,string>::value_type("1-3","2-1"));
n.insert(multimap<string,string>::value_type("1-3","21-4"));
n.insert(multimap<string,string>::value_type("1-3","21-2"));
cout<<"multimap of m"<<endl;
cout<<"--------------"<<endl;
for(multimap<string,string>::iterator i=m.begin();i!=m.end();i++)
cout <<i->first<<" "<<i->second<<endl;
cout<<"multimap of n"<<endl;
cout<<"--------------"<<endl;
for(multimap<string,string>::iterator i=n.begin();i!=n.end();i++)
cout <<i->first<<" "<<i->second<<endl;
}
this results in :
multimap of m
--------------
1-2 1-1
1-2 1-2
1-2 1-3
1-2 1-4
1-3 2-1
1-3 21-1
1-3 21-2
multimap of n
--------------
1-2 1-1
1-2 1-2
1-2 1-5
1-2 1-7
1-3 2-1
1-3 21-4
1-3 21-2
i want to create a new multimap: which has the below elements:
1-2 1-1
1-2 1-2
1-3 2-1
1-3 21-2
EDIT:
also is there a way where i can delete the common elements( pair) from both the maps.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…