[英]compare two multimaps c++
我有兩個multimap.i想創建一個新的multimap,它在給定的兩個multimap中具有公共鍵值對:
例如:
#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;
}
結果是:
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
我想創建一個新的multimap:它具有以下元素:
1-2 1-1
1-2 1-2
1-3 2-1
1-3 21-2
編輯:
還有一種方法可以刪除兩個地圖中的公共元素(對)。
使用<algorithm>
的std::set_intersection
函數模板
std::multimap<T1, T2> newMap;
std::set_intersection(map1.begin(), map1.end(),
map2.begin(), map2.end(),
std::inserter(newMap, newMap.begin());
編輯,是的,顯然這不適用於多圖,而不適用於多圖。 我建議以下內容:
std::multimap<T1, T2> newMap;
std::vector<std::multimap<T1, T2>::value_type> v1(map1.begin(), map1.end());
std::sort(v1.begin(), v1.end());
std::vector<std::multimap<T1, T2>::value_type> v2(map2.begin(), map2.end());
std::sort(v2.begin(), v2.end());
std::set_intersection(v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::inserter(newMap, newMap.begin());
作為Armen(出色的)的替代解決方案, 這是一種同時復制和排序的方法:
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()));
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::inserter(result, result.end()));
並且僅在n
:
result.clear();
std::set_difference(s2.begin(), s2.end(),
s1.begin(), s1.end(),
std::inserter(result, result.end()));
由於您在執行set_difference()
時已將m
和n
(分別復制到s1
和s2
),因此可以clear()
這些並插入其中而不是result
。
有趣的是,這是直接算法,該算法將常見元素移動到新的多圖中,從而更改了原始圖。 您將認識到外循環上排序范圍的標准“設置交集”算法( first
); 並且由於我們不能假設任何有關映射值的內容,因此內循環(關於second
)是對映射值的線性搜索以找到相等值。
template <typename T>
T move_common(T & a, T & b)
{
typename T::const_iterator it1 = a.cbegin(), iend = a.cend(), jt1 = b.cbegin(), jend = b.cend();
T result;
while (it1 != iend && jt1 != jend)
{
if (it1->first < jt1->first)
{
++it1;
}
else if (jt1->first < it1->first)
{
++jt1;
}
else
{
typename T::const_iterator it2 = it1;
while (it2 != iend && it2->first == it1->first)
{
typename T::const_iterator jt2 = jt1;
while (jt2 != jend && jt2->first == jt1->first)
{
if (jt2->second == it2->second)
{
result.insert(*it2);
if (it2 == it1) { a.erase(it1++); it2 = it1; } else { a.erase(it2++); }
if (jt2 == jt1) { b.erase(jt1++); } else { b.erase(jt2); }
break;
}
else
{
++jt2;
if (jt2 == jend || jt2->first != jt1->first) { ++it2; break; }
}
}
}
it1 = it2;
}
}
return result;
}
結果:
common = [(1-2, 1-1), (1-2, 1-2), (1-3, 2-1), (1-3, 21-2)]
n after = [(1-2, 1-3), (1-2, 1-4), (1-3, 21-1)]
m after = [(1-2, 1-5), (1-2, 1-7), (1-3, 21-4)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.