LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Binary/Unary Functors for Sorting/Finding (C++)? (https://www.linuxquestions.org/questions/programming-9/binary-unary-functors-for-sorting-finding-c-326855/)

ta0kira 05-25-2005 01:54 AM

Binary/Unary Functors for Sorting/Finding (C++)?
 
Just a general preference question; when using a functor/function pointer to sort or find elements in a list, would you rather the functor argument be binary (i.e. take 2 arguments and return true/false for less than/equal), or would you rather the functor argument be unary (i.e. take one argument, return another argument, and let the sort/find function do the comparison)?
Code:

struct Structure
{
        int Value;
};


//...would you want to use this to sort:

        bool
LESS_THAN(Structure A, Structure B)
{ return A.Value < B.Value; }

//and these to find:

        bool
EQUAL(Structure A, Structure B)
{ return A.Value == B.Value; }

        bool
EQUAL(Structure A, int B)
{ return A.Value == B; }


//...or this for everything:

        int
GET_VALUE(Structure A)
{ return A.Value; }

//and then let the find/sort function call the comparison operator?

The reason I ask is because my list class currently takes exclusively unary functors, but if a lot of people would prefer binary functors I will change them. I think I am going to change the sort functors to binary only and provide an adapter for users who want to use unary. I will create an adapter so the other type can be used (unary/binary) either way, but I would like the natural functor type to be that which is preferred most. Thanks for your opinion!
ta0kira

ta0kira 05-30-2005 03:12 PM

Binary it is! I really had this decided in my head before I posted, but I was really just looking for objectors. This was a simple change to make, and I think it was better to change it early on. I just posted the updated version of the lib (0.10.0):
http://sourceforge.net/projects/clist-ta0kira/

Thanks for your input and interest.
ta0kira


All times are GMT -5. The time now is 02:36 PM.