I have written a code for finding intersection of two multimap which will compare
values related to key and fetch us a map containing a comman elements (combination
of key and value) from two map. This is displaying one part of string as pointer
and rest as usual.
#include <string>
#include <iostream>
#include <map>
#include <iterator>
#include <stdio.h>
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 : " <<
cout << it2->first << " " << it2->second <<" " << endl;
}
getchar();
}
Expected output :
1 2 22 3
2 1 31 3
Output on console:
0x4483c41 2 22 3
0x4483c42 1 31 3
From your code:
You are outputting
coutitself tocout, which causes the hex value to be printed. You need to either removecoutfrom the second line, or make these two separate statements.The reason that the hex value gets printed is that
basic_ioshas anoperator void*which returns a pointer. This pointer is passed to the overload ofbasic_ostream::operator<<which prints a pointer.