Why would we still need to define our own feature comparator?


In some std library template's parameter, one need to define his/her own function comparator less(a, b), more(a, b) then std::some_template<T, *, myComparator()>, but why?

The purpose of the comparator is to allow sorting of objects in the sorted stl containers. You only need to provide your own comparator if the default one is inadequate for the object type that the container will hold.

For example if you were to make a std::set of the following struct, then you would need to write your own comparator.

struct Person
    std::string first_name, last_name, phone_number;

The default comparator knows how to compare numbers and strings, but it doesn't know how to compare Person objects. This is how one could write a custom comparator for sorting Person objects by last_name.

struct Person_Comparator
    bool operator()(const Person &a, const Person &b) const {
        return a.last_name < b.last_name;