How to write comparator function so that upper_bound work like lower_bound?

how to write comparator function so that upper_bound work like lower_bound?

hello @sagar_aggarwal
it should be ->

bool cmp(int a,int b){

return a<=b;  // a is the value whose lower bound we r searching and b is value of current iterator
}

verify here->

reason-> upper bound comparator pick left portion of array when comparator returns true. otherwise it will pick right portion of array.
and repeat the same on the new portion of array.

do check internal implentation of both lower bound and upper bound there is slight difference.

https://en.cppreference.com/w/cpp/algorithm/upper_bound

https://en.cppreference.com/w/cpp/algorithm/lower_bound

in the internal implementation what is step? it is declared nowhere?

step is mid of array.

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.