0

What should I use? bool compare or sCompare() functor? And why? Are there some differences between using this two options?

struct Dog
{
    int m_age{};
    int m_weigt{};
};

bool compare(const Dog& a, const Dog& b)
{
    return a.m_age > b.m_age;
}

struct sCompare
{
    bool operator()(const Dog& a, const Dog& b)
    {
        return a.m_age > b.m_age;
    }
};

int main()
{
    vector<Dog> dogs{ Dog{1,20}, Dog{2,10}, Dog{3,5}, Dog{10,40} };
    //sort(begin(dogs), end(dogs), compare); this
    //sort(begin(dogs), end(dogs), sCompare()); or this

    return 0;
}
  • 3
    If it's for use at a single place in the code: neither. Use a lambda instead. –  Oct 19 '21 at 19:10
  • Related/duplicate: [Should I use functions or stateless functors?](https://stackoverflow.com/questions/5521898/should-i-use-functions-or-stateless-functors) (although it's super old question, still using `std::binary_function`) – Yksisarvinen Oct 19 '21 at 19:11
  • Also related: [Why use functors over functions?](https://stackoverflow.com/questions/6451866/why-use-functors-over-functions) – user4581301 Oct 19 '21 at 19:12
  • Supposing I have to use it not at a single place in my code. Should I use lambda anyway? – Original nickname Oct 19 '21 at 19:23
  • `operator()` should be `const`. Otherwise there's no difference. – HolyBlackCat Oct 19 '21 at 19:24
  • @Originalnickname I'm a hater of code duplication, but for a simple `X>Y` I could be talked into repeating the lambda because I won't have to run off to read some other part of the file (or another file) for the definition of the functor. That said, repeating the lambda is a perfect opportunity to up and have one with `>` and the other with `<`. You've just seen how easy it is to do that by mistake. – user4581301 Oct 19 '21 at 19:26
  • @user4581301 That makes sense. Thank you a lot! – Original nickname Oct 19 '21 at 19:28

1 Answers1

0

Your two comparators result in opposite ordering (< vs >). Other than that the biggest difference is that you cannot define a function within a function, but you can define a type in a function. Moreover, lambda expressions offer straightforward syntax to do that:

int main()
{
    vector<Dog> dogs{ Dog{1,20}, Dog{2,10}, Dog{3,5}, Dog{10,40} };
    sort(begin(dogs), end(dogs), [](const Dog& a,const Dog& b){ return a.m_age < b.m_age;});
    // or 
    auto comp = [](const Dog& a,const Dog& b){ return a.m_age < b.m_age;}
    sort(begin(dogs), end(dogs),comp); 

}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Yes, right, that was a mistake, The two comparators should be > and > . – Original nickname Oct 19 '21 at 19:16
  • That means I shouldn't ever use functors or functions in this case? – Original nickname Oct 19 '21 at 19:21
  • @Originalnickname The lambda expression is [syntactic sugar](https://en.wikipedia.org/wiki/Syntactic_sugar) for a functor. Usually a functor is preferable to a function because of versatility and better optimization opportunities. – user4581301 Oct 19 '21 at 19:24
  • @user4581301, Thank you very much! – Original nickname Oct 19 '21 at 19:25
  • 1
    If declaring the compare function as a global adds value somewhere else in your project then maybe you could do it. You could instead add an operator < or an inline friend compare() function to your Dog class, but again, only if that adds value to some other part of your code. The lambda is the c++11 way to inject 1-off functors. – Gem Taylor Oct 19 '21 at 19:31