Search code examples
c++boostunordered-setbimapboost-unordered

questions about unordered_multiset_of in boost bimap


I am implementing a boost::bimap and I am considering using unordered_multiset_of but unordered_multiset_of need to pass a hash function and equal operator to it. And I cant get it right.

class MyClass
{
  std::string s1;
  std::string s2;
  bool operator == (MyClass const& myClass)
  {
    return (s1 == myClass.s1 && s2 == myClass.s2);
  }
};

namespace std
{
  template<>
  struct hash<MyClass>
  {
    std::size_t operator()(const MyClass& myClass) const
    {
      std::size_t Seed = 0;
      boost::hash_combine(Seed, myClass.s1);
      boost::hash_combine(Seed, myClass.s2);
      return Seed;
    }
  }
}


int main()
{
  typedef boost::bimaps::bimap<boost::bimaps::unordered_multiset_of<client,std::hash<MyClass>, std::equal_to>, .......................................> MyBiMap;

  MyBiMap MAP;
}

it seems that my hash function and equal_to function gives error. how do I fix it? I believe std::equal_to() will automatically call my == operator defined in MyClass, right?


Solution

  • bool operator == (MyClass const& myClass)

    Must be const bool operator == (MyClass const& myClass)const Othervise std::equal_to will not be able to use it, since it accept const references

    template <class T> struct equal_to : binary_function <T,T,bool> {
      bool operator() (const T& x, const T& y) const {return x==y;}
    };