What's the point of comparison functions in STL?

From: as mellow as a horse (mail_at_MICKmoss42.fslife.co.uk)
Date: 06/27/04


Date: Sun, 27 Jun 2004 16:40:40 +0100

Can't the same thing be achieved through overloaded operators? I notice
there's usually (always?) an stl algorithm that takes a comparison function
and one that doesn't. Is this to get around the problem of classes not
defining an overloaded "<" or whatever, or am I totally missing the point?

I created a vector of integers and tried the min algorithm on that (the one
without the comparison function argument). Worked fine as I'd expect.

I then created a simple class CObject with a public member (int x) and
created a vector containing these (with 'x' initialised to different
values). I tried min() on them as above and wasn't surprised when it kept
returning an iterator to the first object no matter what values I gave them.

So next I overloaded the '<' operator for CObject. I tested this outside of
STL to make sure it worked, and it did. So I repeated the above test and
was disappointed to get the same results. Either min() doesn't use CObjects
'<' operator somehow, or I'm missing the point like I thought.