我正在实现 boost::bimap
并且我正在考虑使用 unordered_multiset_of
但是 unordered_multiset_of
需要传递一个散列函数和相等运算符给它。我做对了。
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;
}
我的散列函数和 equal_to 函数似乎出错了。
我如何解决它?
我相信 std::equal_to()
会自动调用我在 MyClass
中定义的 == 运算符,对吗?
最佳答案
bool 运算符 == (MyClass const& myClass)
必须是 const
bool operator == (MyClass const& myClass)const
否则 std::equal_to
将无法使用它,因为它接受 const 引用
template <class T> struct equal_to : binary_function <T,T,bool> {
bool operator() (const T& x, const T& y) const {return x==y;}
};
关于c++ - 关于boost bimap中unordered_multiset_of的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16151481/