std::lower_bound is a Standard Template Library (STL) algorithm used to find the first position where a given value can be inserted in a sorted range without violating the order. In simple terms, it returns an iterator pointing to the first element that is greater than or equal to (>=) the given value.
Example 1: 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.
The below examples demonstrate some of its common uses.
Example 1: 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.
Example 2: Find lower_bound() in a Vector of String Using Custom Comparator
#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.
Example 3: Find the Existence of an Element in Vector
#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.
Example 4: Find the Number of Smaller and Larger Elements than a Value in 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);
// 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.
Example 5: 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.
Example 6: 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).