简体   繁体   中英

What is the difference between using a “<=” sign instead of a “<” sign in the compare function of an STL in C++?

I have to implement the 3rd parameter, cmp, in the sort() function for sorting an array of integers in descending order .

The problem is that this definition doesn't work properly,

bool cmp (int a, int b)
{
    if(a<b)
        return false;
    else
        return true;
}

However, this does

bool cmp (int a, int b)
{
    if(a<=b)
        return false;
    else
        return true;
}

In the main() function, I use

sort(Arr,Arr+n,cmp);

Please note that I claim that the first code doesn't work properly because my solution to a problem on Codechef is accepted when I use the 2nd, but not with the 1st.

如果两个值相等,则STL比较函数必须返回false。

In sort ,

Last argument:

comp - comparison function object (ie an object that satisfies the requirements of Compare ) ....

Compare

comp(a, b)

Establishes strict weak ordering relation with the following properties:

For all a, comp(a,a)==false

....

That's why

if(a<=b) 
    return false;

works, while

if(a<b)
    return false;

doesn't.

std::sort 's compare function returns a value that indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.

Here is the definition of strict weak ordering from sgi

A Strict Weak Ordering is a binary predicate that compares two objects, returning true if the first precedes the second. This predicate must satisfy the standard mathematical definition of a strict weak ordering. The precise requirements are stated below, but what they roughly mean is that a Strict Weak Ordering has to behave the way that "less than" behaves:

  • if a is less than b then b is not less than a,
  • if a is less than b and b is less than c then a is less than c, and so on.

According to documentation of Compare functions:

Establishes strict weak ordering relation

So, the function comp must define a strict weak ordering. By definition, it must satisfy three conditions:

1) comp(x, x) is false for all x (irreflexivity condition)

2) If comp(x, y) then !comp(x, y) (asymmetry condition)

3) If comp(x, y) and comp(y, z) then comp(x, z) (transitivity condition)

It is easy to see that first example of cmp function does not satisfy the irreflexivity condition, therefore it cannot be used to be passed as comp argument of std::sort . It is also clear that second cmp example in case of x, y are numbers satisfies all of three conditions, so it can be passed as comp argument to std::sort.

The compare function provided to std::sort must return true if its first argument lies before its second argument in the ordering required and false otherwise.

Hence, for descending ordering you should use the > operator>, which for int s returns the complement of what the <= operator returns (which you used to implement your comparison with). Better use std::greater<int> to avoid such pitfalls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM