The std::lower_bound() function is an STL algorithm that returns an iterator pointing to the first element that is greater than or equal to a given value in a sorted range. It is commonly used for searching, finding insertion positions, and performing efficient binary-search-based operations.
- Returns the first element ≥ value in a sorted range.
- Runs in O(log n) time for random-access containers.
- Defined in the <algorithm> header.
Example: Find Lower Bound in a Vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {10, 20, 30, 40, 50};
auto it = lower_bound(v.begin(), v.end(), 30);
cout << *it;
return 0;
}
Output
30
Explanation
- lower_bound() searches for the first element ≥ 30
- Since 30 exists, it returns an iterator pointing to 30
Syntax
iterator lower_bound(iterator first, iterator last, const T& value);
Parameters
- first: Iterator to the beginning of the range
- last: Iterator to one past the last element
- value: Value to be searched
Return Value
- Returns an iterator to the first element ≥ value
- Returns last if no such element exists
Note: If the range is not sorted or at least partitioned with respect to the given value, the behaviour of this function is undefined.
Applications of std::lower_bound()
The following examples demonstrate some common applications.
Find the Lower Bound of a Value in Array
#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;
}
Output
40
Explanation: lower_bound() finds and prints the first element in the sorted array that is greater than or equal to 35, which is 40.
Case-Insensitive Search in a Vector of Strings
#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;
}
Output
banana
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.
Check Whether an Element Exists
#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;
}
Output
40 is found.
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.
Count Smaller and Larger Elements
#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;
}
Output
No. of Smaller Elements: 3 No. of Larger Elements: 2
Explanation: lower_bound() finds the position where 35 would be inserted in the sorted vector; elements before it are smaller, and the remaining elements are larger.
Insert an Element in a Sorted Vector
#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;
}
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
#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;
}
Output
40
Explanation: This code finds and prints the first element in the set that is greater than or equal to 35, which is 40 (though using set::lower_bound() is recommended for better performance).