Boolean compare in video strings STL

in the boolean comparator function, what does return a<b mean ?

Hello @chahatkumar,
We know that every relational statement evalualtes to a boolean value i.e, 0 or 1.
So, a<b is evaluated to 1 if a is less than b otherwise 0.
That means either 0 or 1 is returned.
a< b is used when we need to sort in ascending order.
a > b is used for descending.

okay
but how does this works as in if a<b is used and a is alphabetically less than b,1 gets returned and the function doesnt executes while if a is greater, function executes. is it how this works? or is it something else ?

If your comparator has two arguments int a,int b,
a will be the element stored before b in the array to be sorted.
then while sorting, the sort function checks if the comparator returns 0 or 1.
If the comparator returns 1, then there is no need to interchange the positions of a and b, else swap the position of the two elements being compared.
It has nothing to do with the alphabetical order of a and b. You can use any variable you wish to use as long as you keep in mind that the variable mentioned first in the argument list first will be considered as the first element.

but if it has nothing to do with the alphabetical order,how does it determines whether the value to be returned is 0 or 1?

suppose the array is 7 4 5 2.
and comparator is:
bool comp(int a, int b){
return a<b;
}

Then, while comparing 7 and 4, a becomes 7 and b becomes 4(since a is declared first and 7 is before 4).
here 7>4, therefore a is not less than b. So, 0 is returned.

ohkay thankyouu :smile:

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like