In C++, the std::lower_bound() is a built-in function used for a sorted container and a given value. It mainly finds the position of the first occurrence not less than (either equal or greater) the given value. It is defined inside the <algorithm> header file.
- Works only on sorted containers like set, map, sorted array and sorted vector and assumes sorted in ascending order.
- Using it on an unsorted range can give incorrect results.
- Performs binary search internally, giving a time complexity of O(log n) which is efficient for large datasets.
- Example 1: Input vector = {5, 15, 25, 35, 45} , key = 28, Output: 35 (First element not less than 28)
- Example 2: Input vector ={10, 20, 30, 40, 50} , key = 60, Output: No element found (iterator points to end of vector) , indicates all elements are smaller than the key.
C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<int> v = {10, 20, 30, 40, 50}; // Finding lower bound for value 35 in vector v cout << *lower_bound(v.begin(), v.end(), 35); return 0; }
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
C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int arr[5] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); // Finding lower bound for value 35 in array arr cout << *lower_bound(arr, arr + n, 35); return 0; }
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
C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; // Custom comparator for case-insensitive comparison bool comp(const string &a, const string &b) { return lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), [](char c1, char c2) { return tolower(c1) < tolower(c2); }); } int main() { vector<string> v = {"Apple", "banana", "Cherry", "date", "Elderberry"}; // Finding lower bound of "Avocado" auto lb = lower_bound(v.begin(), v.end(), "Avocado", comp); // Checking if lower bound is found if (lb != v.end()) cout << *lb; else cout << "Lower bound not found!"; return 0; }
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
C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<int> v = {10, 20, 30, 40, 50}; int val = 40; // Cheking if val exists in the vector v auto it = lower_bound(v.begin(), v.end(), val) if (*it == val) cout << val << " is found."; else cout << val << " is NOT found."; return 0; }
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
C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<int> v = {10, 20, 30, 40, 50}; int val = 35; // Finding lower and upper bound of val in v auto lb = lower_bound(v.begin(), v.end(), val); // Finding the number of smaller elements cout << "No. of Smaller Elements: " << lb - v.begin() << endl; // Finding the number of larger elements cout << "No. of Larger Elements: " << v.size() - (lb - v.begin()); return 0; }
OutputNo. 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
C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<int> v = {10, 20, 30, 40, 50}; int val = 35; // Finding lower and upper bound of val in v auto lb = lower_bound(v.begin(), v.end(), val); // Inserting 35 to its correct position v.insert(lb, val); for (auto i : v) cout << i << " "; return 0; }
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
C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { set<int> s = {10, 20, 30, 40, 50}; // Finding lower bound for value 35 in array arr cout << *lower_bound(s.begin(), s.end(), 35); return 0; }
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.
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems
My Profile