#include <cstring>
#include <iostream>
#include <map>
#include <iterator>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
//using namespace std;
template <class InputIterator1, class InputIterator2, class OutputIterator>
/* templet function to return common elements od two map */
OutputIterator map_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result )
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) ++first1;
else if (*first2<*first1) ++first2;
else {
const char*abc= new char[15];
const char*def= new char[15];
/* Below code inserts the common element in result map */
if(strcmp((first1->second.c_str()),(first2->second.c_str()))==0)
{
result.insert(std::pair<std::string,std::string>(first1->first,first1->second));
++first1;
++first2;
}
else
{
++first1;
++first2;
}
}
}
return result;
}
using namespace std;
int main()
{
std::multimap<string, string> m;
std::multimap<string, string>::iterator it2;
std::multimap<string, string> intersection;
std::multimap<string, string> common;
std::multimap<string, string> it3;
cout <<"Making Map m "<<endl;
m.insert(pair< string, string>("1 2"," 22 3" ));
m.insert(pair< string, string>("1 2"," 21 4" ));
m.insert(pair< string, string>("2 1"," 31 3" ));
cout <<"Making Map c "<<endl;
std::multimap<string, string> c;
c.insert(pair< string, string>("1 2"," 22 3" ));
c.insert(pair< string, string>("1 2"," 22 4" ));
c.insert(pair< string, string>("2 1"," 31 3" ));
cout << "Elements in common map are: " << endl;
it3=map_intersection (m.begin(), m.end(), c.begin(), c.end(),common);
cout << " am i out of the map_intersection loop " << endl;
cout << " size of common map is : " << it3.size()<< endl;
for(it2 =it3.begin(); it2!=it3.end();it2++)
{
cout << "first common element is : " << endl;
cout<< it2->first << " " << it2->second <<" " << endl;
}
getchar();
}
我遇到了一个代码,它找到两个多图的交集,其值分配给不同的多图。我想要一个函数,它只检查两个多图的共同值并将第一个多图的键分配给交集。例如,
多图 1:- 1 2 22 3 2 1 31 3 多图2:- 1 3 22 3 2 1 31 3 输出:- 2 1 31 3