Binary Search STL doubt

Can you explain the behavior of the code when I enter key as 41. I did not understand the output of the upper and lower bound functions in this code.

Code -https://ide.codingblocks.com/s/403415

hello @PranavM16 the ouput for 41 is coming out to be same because 41 is not present in the list so and in the range it is lying betwen 40 and 50 so the upperbound and lower bound both are giving the same answer.
if you still have any doubt you can ask here:
Happy Learning!!

Why does lower bound give 50? Shouldn’t it give 40? Maybe this is an exception for this weird behavior

Hello @PranavM16 no lower bound means that you at which index you are finding that element which is less then or equal to key.
it is mention there in the comment as well in the code:
if you still have any doubt you can ask here:
Happy Learning!!

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