How can I use a custom type as key for a map in C++?

I suspect you need

bool operator<(const Foo& foo1) const;

Note the const after the arguments, this is to make “your” (the left-hand side in the comparison) object constant.

The reason only a single operator is needed is that it is enough to implement the required ordering. To answer the abstract question “does a have to come before b?” it is enough to know whether a is less than b.

Leave a Comment