Comparators in C++

Hey, I recently got the concept of comparators in C++ in sorting
but i think there is something I didn’t get about it.
See here, my 3 different submissions for the same problem

No Comparator got accepted : CodeChef: Practical coding for everyone

Comparator function got error :
https://www.codechef.com/viewsolution/67594930
https://www.codechef.com/viewsolution/67594606

Can anyone please explain me where I am wrong, and what’s the problem in my code;

bool cmp (pii &e1, pii &e2) 
{
    return (e1.ff <= e2.ff);
}

Try replacing that with

bool cmp (pii &e1, pii &e2)
{
    return (e1.ff < e2.ff);
}

Reason: sorting - sort function C++ segmentation fault - Stack Overflow

1 Like

but it will change my logic then
and also i am passing them by reference but still in last testcase it is showing segmentation fault

and sorry i am responding too late :sweat_smile:

The logic remains the same. There is no need to write a separate comparator if your comparator does the same as the default comparator. Here’s an accepted code: CodeChef: Practical coding for everyone