0

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 
3
  • "Template function written for finding multimap intersectionof written one part of string as pointer and rest correctly": StackOverflow should offer a bronze badge for the most convoluted and at the same time zero-information-content title. Commented Jan 1, 2012 at 15:47
  • Nitpick: C++ actually doesn’t know what a “template function” is (although even the standard mentions it by accident, if I recall correctly). You mean a “function template”. There’s a small but fundamental conceptual difference. Commented Jan 1, 2012 at 15:56
  • There was a similar problem a while ago, check it out. Commented Jan 1, 2012 at 16:02

1 Answer 1

2

From your code:

cout << "first common element is : " << cout << it2->first << " " << it2->second <<" " << endl; ^^^^ 

You are outputting cout itself to cout, which causes the hex value to be printed. You need to either remove cout from the second line, or make these two separate statements.

The reason that the hex value gets printed is that basic_ios has an operator void* which returns a pointer. This pointer is passed to the overload of basic_ostream::operator<< which prints a pointer.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks interjay , that was a mistake i overlooked . Thanks a lot
@mikeNakis Dude everyone is not a shakespeare as you , but i would rather try to be more informative in writting the heading , sorry if inconvenience caused .
@konard Rudolph Thanks for your effort .. Regards
@kerrek SB ... Thanks for your effort .. Regards

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.