Use of comparator in lower_bound function

how return a<=b working in lower_bound function as initially a will be 1 and b will be 168,so it should return 1 to the lower bound function.the condition will hold true in first run of function and answer should be 1 not 100

@KetanPandey,
I think you are confused about working of comparator, in lower_bound a<=b does not return a or b, it returns a boolean value which is true if a<=b, else false, which indeed tells the lower_bound function to go right or left.

sir,i know it returns a bool value,but i am confused how it can return previous value,when money=168,when function wiill be at 200 than it will come back and return 100,how that is possible.Actually i also want to know unternal working of this function.Also tell me why are we subtracting 1 from lb as function already returns correct answer

@KetanPandey,
What do you mean by “function will be at”, its a binary search function it works on ranges, not individual elements.
Internal working is just a simple binary search over sorted array, the comparator just tells the function to search left or right.
Subtracting 1 may not seem very important here, but it is a pretty common technique, and you will know why we use it, ones you solve enough problems.

ok,i have got this,but i still have a doubt how the address that lower_bound function is returning is that of 200 in case of both inputs 168 and 100,that’s why we are subtracting -1 from the value.

@KetanPandey,
If the result remains same(assuming it is as you are saying), you can choose not to subtract 1. Anything works, as far as the result is correct. -1 technique was just to teach students, that something like that exists and can be used where needed.

Sir ,plz don’t assume .I want to say that the value returned by lower_bound function is 200 for both inputs,Why it is not 100 as it the value returned should be less than/equal to input

@KetanPandey,
less than equal to is when we use the built in comparator, here we have used a custom comparator. The best way to get a feeling of what’s happening is to implement it yourself. Implement a binary search yourself, which finds first occurrence of value>=given value in sorted array. You will understand, how comparator affects going left or right in binary search.

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.