ssk
ssk

Reputation: 9255

map comparator operator overloading

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.

Upvotes: 0

Views: 1290

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490108

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 overload operator< for your key type.

Upvotes: 3

Related Questions