How to use lower_boundupper_bound to find position of any number in array?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
In C++, the Standard Template Library (STL) provides lower_bound and upper_bound as efficient tools for searching within sorted arrays. Both functions use binary search internally, giving you O(log n) performance. Understanding how they differ and when to use each one is essential for solving search, counting, and insertion problems effectively. This article walks through the mechanics of both functions with concrete examples.
How lower_bound Works
The lower_bound function is declared in the <algorithm> header. It searches a sorted range and returns an iterator pointing to the first element that is not less than the target value. In other words, it finds the first position where the target could be inserted without breaking the sort order.
Syntax
firstandlastdefine the sorted range to search.valueis the target you are looking for.- The function returns an iterator to the first element >=
value.
Example
Since the first 20 in the array sits at index 1, lower_bound returns an iterator pointing there. If the value does not exist in the array, lower_bound returns the position where it would be inserted.
How upper_bound Works
The upper_bound function is similar but returns an iterator pointing to the first element that is strictly greater than the target value.
Syntax
Example
The first element strictly greater than 20 is 30 at index 4. This is the key difference between the two functions: lower_bound finds >= while upper_bound finds strictly >.
Using Both Together
Combining lower_bound and upper_bound unlocks several useful patterns.
Counting Occurrences of a Value
The distance between upper_bound and lower_bound gives you the count of a specific value in a sorted array.
You can also use std::equal_range which returns both iterators in a single call, performing two binary searches internally.
Finding Insertion Position
If you want to insert a new element while keeping the array sorted, lower_bound gives you the correct insertion point.
Checking if a Value Exists
You can use lower_bound to perform a membership check by verifying that the returned iterator points to an element equal to your target.
This is what std::binary_search does internally, but using lower_bound directly gives you the position as well.
Using Custom Comparators
Both functions accept an optional comparator for custom sort orders, such as descending arrays or sorting by a struct field.
Common Pitfalls
- Unsorted input: Both functions require the range to be sorted according to the comparator. Using them on unsorted data produces undefined behavior with no compiler warning.
- Confusing the two: Remember that
lower_boundfinds >=, whileupper_boundfinds strictly >. Mixing them up leads to off-by-one errors. - Iterator arithmetic on non-random-access containers: The subtraction
it - arr.begin()works in O(1) for vectors and arrays, but forstd::setorstd::listyou should usestd::distanceinstead. - Empty ranges: Both functions handle empty ranges correctly by returning
last, but forgetting to check for this can cause dereferencing issues.
Summary
lower_bound and upper_bound are powerful STL functions that use binary search to locate positions in sorted arrays in O(log n) time. lower_bound finds the first element >= the target, while upper_bound finds the first element strictly > the target. Together they enable counting occurrences, finding insertion points, and performing membership checks efficiently. Always ensure your data is sorted before calling these functions, and use custom comparators when working with non-default sort orders.
Related reading
- How to use recursion in creating a binary search algorithm
- How to use to find files recursively?
- how to use weight when training a weak learner for adaboost
- How to walk binary abstract syntax tree to generate infix notation with minimally correct parentheses
- How to use null in switch
- How to work with TF Lite library in a c project
- How to work out the complexity of the game 2048?
- How to write a function to generate random number 0/1 use another random function?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.