Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why std::sort() requires static Compare function? [duplicate]

Tags:

c++

std

I am solving a problem in Leetcode OJ. I wrote a solution like this:

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    bool comparefunc (const Interval& a, const Interval& b) {
        return a.start < b.start;
    }
    vector<Interval> merge(vector<Interval> &intervals) {
        vector<Interval> result;
        if(intervals.empty()) return result;

        // sort Interval vector on increasing order of start
        sort (intervals.begin(), intervals.end(), comparefunc);

        // some other stuffs
        result.push_back(intervals[0]);
        for(int i = 1; i < intervals.size(); ++i) {
            if(intervals[i].start > result.back().end) result.push_back(intervals[i]);
            else result.back().end = max(result.back().end, intervals[i].end);
        }
        return result;
    }
};

And this yields compilation error:

    no matching function for call to 
'sort(std::vector<Interval>::iterator, std::vector<Interval>::iterator, <unresolved overloaded function type>)'

Then I changed the comparefunc signature with static(saw in other's solution) like:

static bool comparefunc (const Interval& a, const Interval& b) {
    return a.start < b.start;
}

And it worked! My question is - why it needs to be static?

like image 638
Kaidul Avatar asked Sep 30 '14 22:09

Kaidul


People also ask

Why we use static in comparator function in c++?

Static Comparator function for sort (Reasons as to why?) C++ So for those who're getting a 'RunTime Error' while executing a solution having std::sort with a comparator function and are in a hurry , just add the static keyword before your comparator function,making it a static member function, and you're good to go.

Why does the std::sort receive a function?

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. The function can also​ be used for custom sorting by passing in a comparator function that returns a boolean.

What is static member and static member function in C++?

The static member functions are special functions used to access the static data members or other static member functions. A member function is defined using the static keyword. A static member function shares the single copy of the member function to any number of the class' objects.

How does comparator work in C++?

The comparator class compares the student to be searched from the list of students on the basis of their name attribute. If the name attribute of the object to be searched is equal to any of the object's name attribute in the list then it returns true, otherwise, it returns false.


2 Answers

Think of how you call compareFunc outside of the class. You always would have something like

a.compareFunc(b, c)
^             ^  ^

which is 3 parameters, not 2.

sort's code is outside your class and would have to use the syntax above.

Making the member static allows this call:

Solution::compareFunc(a, b)

which is only 2 parameters and matches the predicate std::sort expects.

This is why (for example) when you overload operator< as a member function it accepts one parameter, whereas if you overload it as a nonmember, it requires two:

struct Foo
{
    bool operator<(Foo const& other) { /* ... */ }
};

int main()
{
    Foo a, b;
    a < b; // calls a.operator<(b)
}

versus

struct Foo
{};

bool operator<(Foo const& lhs, foo const& rhs) { /* ... */ }

int main()
{
    Foo a, b;
    a < b; // calls operator<(a, b)
}
like image 124
Billy ONeal Avatar answered Oct 17 '22 19:10

Billy ONeal


without static, &Solution::comparefunc's type is:

bool (Solution::*) (const Interval& a, const Interval& b);

with static, &Solution::comparefunc's type is:

bool (*) (const Interval& a, const Interval& b);
like image 5
Jarod42 Avatar answered Oct 17 '22 17:10

Jarod42