fork download
  1. #include <map>
  2. #include <string>
  3.  
  4. #include <stdio.h>
  5.  
  6. class FOO
  7. {
  8. public:
  9. FOO() { printf("FOO::FOO()\n"); }
  10. FOO(const FOO&) { printf("FOO::FOO(const FOO &)\n"); }
  11. FOO& operator=(const FOO &) { printf("FOO::oprartor=(const FOO&)\n"); return *this; }
  12. ~FOO() { printf("FOO::~FOO\n"); }
  13.  
  14. bool operator<(const FOO&) const { return "FOO::operator<(const FOO&)\n"; }
  15. bool operator>(const FOO&) const { return "FOO::operator>(const FOO&)\n"; }
  16. bool operator<=(const FOO&) const { return "FOO::operator<=(const FOO&)\n"; }
  17. bool operator>=(const FOO&) const { return "FOO::operator>=(const FOO&)\n"; }
  18. bool operator==(const FOO&) const { return "FOO::operator==(const FOO&)\n"; }
  19. bool operator!=(const FOO&) const { return "FOO::operator!=<(const FOO&)\n"; }
  20. };
  21.  
  22. class BAR
  23. {
  24. public:
  25. BAR() { printf("BAR::BAR()\n"); }
  26. BAR(const BAR&) { printf("BAR::BAR(const BAR &)\n"); }
  27. BAR& operator=(const BAR &) { printf("BAR::oprartor=(const BAR&)\n"); return *this; }
  28. ~BAR() { printf("BAR::~BAR\n"); }
  29.  
  30. bool operator<(const BAR&) const { return "BAR::operator<(const BAR&)\n"; }
  31. bool operator>(const BAR&) const { return "BAR::operator>(const BAR&)\n"; }
  32. bool operator<=(const BAR&) const { return "BAR::operator<=(const BAR&)\n"; }
  33. bool operator>=(const BAR&) const { return "BAR::operator>=(const BAR&)\n"; }
  34. bool operator==(const BAR&) const { return "BAR::operator==(const BAR&)\n"; }
  35. bool operator!=(const BAR&) const { return "BAR::operator!=<(const BAR&)\n"; }
  36. };
  37.  
  38. int main(void)
  39. {
  40. typedef std::pair<FOO, BAR> FooBar;
  41. typedef std::pair<const FOO&, const BAR&> FooBarRef;
  42.  
  43. std::map<FooBar, int> myMap;
  44. FOO foo;
  45. BAR bar;
  46.  
  47. printf("\nMethod 1:\n");
  48. myMap.insert(std::make_pair(FooBar(foo, bar), 1));
  49. printf("Done\n\n");
  50.  
  51. myMap.clear();
  52.  
  53. printf("\nMethod 2:\n");
  54. myMap.insert(std::make_pair(FooBarRef(foo, bar), 1));
  55. printf("Done\n\n");
  56. }
  57.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
FOO::FOO()
BAR::BAR()

Method 1:
FOO::FOO(const FOO &)
BAR::BAR(const BAR &)
FOO::FOO(const FOO &)
BAR::BAR(const BAR &)
FOO::FOO(const FOO &)
BAR::BAR(const BAR &)
FOO::FOO(const FOO &)
BAR::BAR(const BAR &)
BAR::~BAR
FOO::~FOO
BAR::~BAR
FOO::~FOO
BAR::~BAR
FOO::~FOO
Done

BAR::~BAR
FOO::~FOO

Method 2:
FOO::FOO(const FOO &)
BAR::BAR(const BAR &)
FOO::FOO(const FOO &)
BAR::BAR(const BAR &)
BAR::~BAR
FOO::~FOO
Done

BAR::~BAR
FOO::~FOO
BAR::~BAR
FOO::~FOO