I am trying to use the Comparator for map’s key comparison.
std::map<SomePointer,SomeValue, SomeComparator> testMap;
class SomeComparator
{
SomeComparator( ){ }
bool operator()( const SomePointer& sp1, const SomePointer& sp2) const
{
return sp1 == sp2;
}
}
My question is whether I need to overload the operator( ) or something else for allowing the map::find to use the appropriate comparator for comparing two pointers and retrieve the matching one.
For map you need to provide a
<type of comparison — a “strict weak ordering”, to be specific. It deduces equality when A is not less than B and B is not less than A.But, to answer the question you actually asked: if you’re providing a comparator functor as above, you overload
operator()for that functor. Otherwise, you can overloadoperator<for your key type.