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
?
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.
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.
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.
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.
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)
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With