non mutating algorithm Question 8

Last Updated :
Discuss
Comments

Binary search is internally implemented using lower_bound() as shown:

it = lower_bound(begin, end, x);
if (it != end && !(x < *it))
return true;
else
return false;

How to modify this logic to make it work with upper_bound()?

it = ++upper_bound(begin, end, x);

it = --upper_bound(begin, end, x);

it = upper_bound(begin, end, x);

Not Possible to do it with upper bound

Share your thoughts in the comments