why we use compare function and still subtracting 1. shouldn’t the statement (return a<=b) imply element in coin array less than or equal to money (opposite of >= used in lower bound).
when we put money = 200, the lower_bound function gives 500, please explain.
Money change using binary stl
hi @skaushik.46 , I think you have misunderstood about the working of the comparator used for lower bound function,
the lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than value passed to the function.
so what happens with comparator function is the function will return an iterator for first element which comparator function returns false.
So, the comparator used here is :-
bool comparator(int a,int b){
return a<=b;
}
b will be the key and a will be the elements of the array
so if we use 200 , our lower_bound function comparator will give us index for 500 (after subtracting base address),since 500 is the first value encountered for which 500<=200 is false and after subtracting 1 from it we get index for 200.
similarly for 168, the first element we get false from comparator function will be 200 . and thus after subtracting we get index for 100.
you can refer to this discussion for some more info on comparator function :-
I have corrected your code here :-
This might be little tricky to understand at first , So , try to run some examples for more clarity and In case of any doubt feel free to ask 