Demystifying S.T.L. functions upper_bound() & lower_bound();

catchy picture to gather views.

There are a lot of articles/documentations on the internet that start with the defintion of upper_bound() & lower_bound(), but I don’t want to indulge in all that bare talking. We’ll come to the defintions, but not right now.

Lets dive directly into the examples :

well, upper_bound() returns an element from the array that is just greater than val.

cpp program
output
val : 40
upper_bound for val = 40

So, upper_bound() returns an iterator pointing to the first element in the range [first, last) that is greater than value, or end of the array if no such element is found.

Now, let’s talk about lower_bound(), and once again let’s dive directly into the examples,

well, lower_bound returns an element that is just not less than the given val, in simple words, if val is found it returns val otherwise it returns the element just greater than val.

dummy cpp program
output
dummy cpp program
output

--

--

I write articles on Android Development, and problem solving.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store