Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining < for STL sort algorithm - operator overload, functor or standalone function?

I have a stl::list containing Widget class objects. They need to be sorted according to two members in the Widget class.

For the sorting to work, a less-than comparator comparing two Widget objects must be defined. There seems to be a myriad of ways to do it. From what I can gather, one can either:

a. Define a comparison operator overload in the class:

bool Widget::operator< (const Widget &rhs) const

b. Define a standalone function taking two Widgets:

bool operator<(const Widget& lhs, const Widget& rhs);

And then make the Widget class a friend of it:

class Widget {
    // Various class definitions ...
    friend bool operator<(const Widget& lhs, const Widget& rhs);
};

c. Define a functor and then include it as a parameter when calling the sort function:

class Widget_Less :
public binary_function<Widget, Widget, bool> { 
    bool operator()(const Widget &lhs, const Widget& rhs) const;
};

Does anybody know which method is better? In particular I am interested to know if I should do 1 or 2. I searched the book Effective STL by Scott Meyer but unfortunately it does not have anything to say about this.

Thank you for your reply.

like image 631
Andy Avatar asked Mar 13 '10 00:03

Andy


People also ask

Which algorithm is used in sort function in STL?

In more details it is implemented using hybrid of QuickSort, HeapSort and InsertionSort.By default, it uses QuickSort but if QuickSort is doing unfair partitioning and taking more than N*logN time, it switches to HeapSort and when the array size becomes really small, it switches to InsertionSort.

Which operators are overloaded for functors?

Which of the following operators are overloaded for functors? Explanation: () operator is overloaded to use functor property in a C++ program because this is the only operator used for a function call.

What should I include in std::sort?

std::sort() in C++ STL. It generally takes two parameters, the first one being the point of the array/vector from where the sorting needs to begin and the second parameter being the length up to which we want the array/vector to get sorted.

Where is std::sort defined?

std::sort() is a built-in function in C++'s Standard Template Library. The function takes in a beginning iterator, an ending iterator, and (by default) sorts the iterable in ascending order.


1 Answers

If you are only comparing two Widgets to each other, use a member operator <. If you are comparing Widget to something else, define a global operator < (the two parameter version, optionally a friend of the Widget class but that is a separate issue.

Functor you really only want if you are doing something a little less orthodox. Choose a functor if a "less than" comparison doesn't make sense in the context of widgets. In that case, having operator < could be confusing. Of course, functors still have to provide an ordering, but just because it is an ordering doesn't really mean it is a "less than" operation. (Example, sorting states by population is probably better for a functor than an operator <.

like image 180
SoapBox Avatar answered Nov 10 '22 01:11

SoapBox