Possibly Wrong Code

The use of compare function function with lower bound should return a number less than or equal to key but its returning strictly greater than than the key. Although the answer comes out to be correct because we are doing - 1 in the index, this is similar of doing of upper bound - 1 !! Please correct me if i am wrong

@chikugoel1998,
Yes you are correct, if we had used lower_bound without custom comparator, it would have returned iterator to number equal to or just greater than key, but because of the custom comparator, it is returning iterator to strictly greater number, which is equivalent to upper_bound without custom comparator.

So why isn’t comparator working and what should be correct code/syntax for it ?. Also please correct in the course as well.

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.

@chikugoel1998,
I think there is a confusion, I meant to say, the comparator is working fine, bhaiya has used >=, this basically made the lower_bound work like upper_bound, that’s it. What I meant to say was that, if bhaiya had used upper_bound function without any comparator, it would still give the required answer, but with using lower_bound, a comparator was required, which made the lower_bound return an index which is strictly greater than “money” (which is equivalent to upper_bound).

So, for your doubt, comparator is working, the code/syntax is correct.

Firstly bhaiya has used <=
Secondly >= is lower bound property, upper bound is strictly greater than the number/key
So how its correct ??

@chikugoel1998,
I think you are confused with the working of comparators in general. Check this out


Link here

The default comparator in lower_bound is not return a<=b, rather its return a<b. Making it return a<=b, is helping us in getting the index required here, nothing else.

For understanding, the difference between lower_bound and upper_bound, I would suggest try implementing both function by yourself, basically the difference lies in the fact that in lower_bound when you get an element = key value, you still go left, but in upper_bound, when you get = key value, you go right.

hi @chikugoel1998 do you have any further query?