0
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

bool compare (int a, int b)
{
    return a>b;
}

int main() 
{
    int arr[5]= {1,2,5,9,6};
    sort (arr,arr+5, compare);
    
    for (auto i: arr)
    cout << i << " ";

    return 0;
}

The above code is sorted in descending order. I am completely blank on how it is working. I read articles but did not get a good explanation.

  1. Why compare is not followed by () while calling as we do with functions?
  2. How does the compare function get the value as we do not have passed any parameters?
  3. How do return true and false results in descending order sorting?
Shroud
  • 35
  • 7
  • 3
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) And while [`using namespace std;` is a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) its sometimes okay for small examples like this one. – Some programmer dude Aug 05 '22 at 08:20
  • *I read articles but did not get a good explanation* -- Where are these articles that you read? Good article(s) (better to use books) will explain in detail the issues you are having understanding the code. – PaulMcKenzie Aug 05 '22 at 08:21
  • You pass a pointer to the function `compare` to the `std::sort` function. Then `std::sort` will call that function to compare to values. Regarding point 3, the ordering function returns `true` if `a` should come before `b`. See e.g. [this `std::sort` reference](https://en.cppreference.com/w/cpp/algorithm/sort) for how it uses the ordering function. – Some programmer dude Aug 05 '22 at 08:22
  • Use these : https://www.learncpp.com/cpp-tutorial/introduction-to-standard-library-algorithms/ sort is one of them. And that site might teach you some other C++ things as well. (Or as paul said, ge a recent book) – Pepijn Kramer Aug 05 '22 at 08:35
  • @Someprogrammerdude It's never OK. – Paul Sanders Aug 05 '22 at 08:43

2 Answers2

1
  1. Because you are not calling it. You are passing the compare function to std::sort without calling it, so that std::sort can call it for you.

  2. It is called by std::sort (many times), and std::sort supplies the parameters from your array

  3. Again std::sort looks at the return value (true or false) and sorts the two numbers accordingly.

john
  • 85,011
  • 4
  • 57
  • 81
  • The third parameter return either true or false and on the basis of a boolean value, it sorts in ascending and descending order – Shroud Aug 05 '22 at 09:14
  • @Shroud No that's not right. The boolean value specifies how to order the two parameters. It's nothing to do with ascending or descending order. – john Aug 05 '22 at 09:24
  • @Shroud For example if the two parameters are equal the comparison function should return false **always**. Doesn't matter whether you are trying to do ascending or descending order. – john Aug 05 '22 at 09:26
1

In C++, you can pass function pointers as parameters. You can define you custom function which receives function pointer as argument.

int my_func(int a, std::string(*fptr)(int))
{
    //whatever
    .
    .
    return some_int;
}

Here my_func accepts pointer to function which returns std::string and accepts one argument of type int.

std::sort also accepts function pointer, which is expected to return bool, or at least something the return value of which is convertible to bool, and two arguments to compare. If you want to know how that is used in std::sort implementation, you can look at any other function in algorithm header which accepts some function. For example look at the possible implementation of std::copy_if(From here).

template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last, 
                 OutputIt d_first, UnaryPredicate pred)
{
    for (; first != last; ++first) {
        if (pred(*first)) {
            *d_first = *first;
            ++d_first;
        }
    }
    return d_first;
}
Karen Baghdasaryan
  • 2,407
  • 6
  • 24