比较两个多图形c ++ [英] compare two multimaps c++

查看:147
本文介绍了比较两个多图形c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个multimaps.i想要创建一个新的multimap在给定的那些两个多图表中有公共的键值对:



例如: / p>

  #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;

}

这会导致:

 多图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

我要创建一个新的multimap:其中包含以下元素:

  1-2 1-1 
1-2 1-2
1-3 2-1
1 -3 21-2

编辑

$ b $

解决方案

还有一种方法可以从两个映射中删除公共元素

作为 Armen's(这是极好的)的替代解决方案,这里有一个方法来同时复制和排序:

  typedef std :: multimap< std :: string,std :: string> 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< map_type :: value_type> s1(m.begin(),m.end());
std :: set< map_type :: value_type> s2(n.begin(),n.end());
std :: set_intersection(s1.begin(),s1.end(),
s2.begin(),s2.end(),
std :: insertionter结束()));



输出:



  intersection 
============
1-2 1-1
1-2 1-2
1-3 2 -1
1-3 21-2

要获取仅在 m

  result.clear 
std :: set_difference(s1.begin(),s1.end(),
s2.begin(),s2.end(),
std :: insertionter结束()));

且仅在 n

  result.clear(); 
std :: set_difference(s2.begin(),s2.end(),
s1.begin(),s1.end(),
std :: insertionter结束()));



由于您已复制 m n在执行 s1 s2 http://www.cplusplus.com/reference/algorithm/set_difference/rel =nofollow> set_difference() ,您可以 clear()这些并插入到它们中,而不是 result


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.

解决方案

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<std::string, std::string> 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<map_type::value_type> s1(m.begin(), m.end());
std::set<map_type::value_type> s2(n.begin(), n.end());
std::set_intersection(s1.begin(), s1.end(), 
                      s2.begin(), s2.end(), 
                      std::inserter(result, result.end()));

Output:

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.

这篇关于比较两个多图形c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆