Open In App

std::lower_bound in C++

Last Updated : 13 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Share
226 Likes
Like
Report

In C++, the std::lower_bound() is a built-in function used to find the position of an element in a sorted range that has a value not less than the given value. It is defined inside the <algorithm> header file. In this article, we will learn about std::lower_bound() function in C++.

Example:


Output
40

Syntax of lower_bound()

std::lower_bound (first, last, val, comp);

Parameters

  • first: Iterator to the first element in the range.
  • last: Iterator to the theoretical element just after the last element in the range.
  • val: Value to be compared.
  • comp(optional): Binary function that accepts and compares val with an element in the range. By default, it returns true if the element in the range is smaller than val, false otherwise.

Return Value

  • Returns an iterator to the smallest number greater than or equal to val.
  • If all the elements in the range are less than the given value, returns iterator to the end of the range.
  • If all the elements in the range are greater than given value, returns iterator to the beginning of the range.

Note: If the range is not sorted or at least partitioned with respect to the given value, the behaviour of this function is undefined.

The std::lower_bound function is useful for finding the first position where a value can be inserted in a sorted range.

More Examples of std::lower_bound()

The std::lower_bound() function is an interesting function that can be used for number of applications. The below examples demonstrate some of its common uses.

Find the Lower Bound of a Value in Array


Output
40

Time Complexity: O(log n), where n is the number of elements in the array.
Auxiliary Space: O(1)

Find lower_bound() in a Vector of String Using Custom Comparator


Output
banana

Time Complexity: O(log n), where n is the number of elements in the vector.
Auxiliary Space: O(1)

Explanation: We need to use the custom comparator function to perform the case insensitive search as the default comparator treats the uppercase and lowercase differently.

Find the Existence of an Element in Vector


Output
40 is found.

Time Complexity: O(log n), where n is the number of elements in vector.
Auxiliary Space: O(1)

Explanation: The lower_bound() function will return the iterator to the given value if it is present in the vector. If it is not present, it will return iterator to the largest element smaller than the given value.

Find the Number of Smaller and Larger Elements than a Value in Vector


Output
No. of Smaller Elements: 3
No. of Larger Elements: 2

Time Complexity: O(log n), where n is the number of elements in vector.
Auxiliary Space: O(1)

Explanation: The lower_bound() function will return the iterator to the first element just greater than or equal to the given value in the vector. As the vector is sorted, all the elements previous to this iterator will be less than the give value, so we can subtract the vector.begin() iterator from the iterator returned by the lower_bound() to get the number of smaller elements. We can then find the number of greater elements by subtracting this number from the total number of elements.

Insert an Element in a Sorted Vector


Output
10 20 30 35 40 50 

Explanation: The lower_bound() returns the position by which the vector is partitioned with respect to the given value. In other words, if the element was present in the sorted vector, it would be present at this position.

Finding the Lower Bound in a Set


Output
40

Time Complexity: O(n)
Space Complexity: O(1)

Explanation: The time complexity is O(n) for the set is because it doesn't provide random access to its elements. So, the lower_bound() function have to increment it sequentially to find the middle element (part of binary search algorithm) each time leading to increased time complexity. However, std::set have their own specialized version named as set::lower_bound() method.


Lower Bound in C++STL
Visit Course explore course icon
Practice Tags :

Similar Reads