Home C++ Algorithms Functions lower_bound() C++ std::lower_bound() Function Definition In C++, std::lower_bound() does a binary search in a sorted range. It returns an iterator to the first element that is greater than or equal to the value.
That makes it the right tool for finding where a value is, or where it should be inserted to keep the range sorted.
Syntax Add #include <algorithm>, then call std::lower_bound(). Start with these two lines.
std::lower_bound(v.begin(), v.end(), 20);
std::lower_bound(v.begin(), v.end(), 20, comp); Call What it does std::lower_bound(v.begin(), v.end(), 20);First element that is 20 or more. std::lower_bound(v.begin(), v.end(), 20, comp);Search with your own ordering.
Library declarations template<class ForwardIt, class T>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value);
template<class ForwardIt, class T, class Compare>
ForwardIt lower_bound(ForwardIt first, ForwardIt last,
const T& value, Compare comp); Parameters Parameter Description first, last Required. A sorted range. value Required. The value to search for. comp Optional. The same comparison that was used to sort the range.
Return Value Return Value Description Iterator Points at the first element not less than value. lastReturned when every element is less than value.
C++ Examples 1. Find the first 20 2. Value not in the vector 3. Insert in sorted order Example 1 — Find the first 20 Find the first element that is at least 20.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 20, 30, 40};
auto it = std::lower_bound(v.begin(), v.end(), 20);
std::cout << "First 20 at index " << (it - v.begin()) << '\n';
return 0;
} How It Works The vector is sorted, so a binary search works. The first element that is not less than 20 is at index 1. There are two 20s, and lower_bound() gives the first one. Example 2 — Value not in the vector Find where a missing value would go.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 20, 30, 40};
auto it = std::lower_bound(v.begin(), v.end(), 25);
std::cout << "Insert 25 before index " << (it - v.begin()) << '\n';
std::cout << "Value there: " << *it << '\n';
return 0;
} Insert 25 before index 3
Value there: 30 How It Works There is no 25, so the search returns the first larger element. That is 30 at index 3. Inserting at that position keeps the vector sorted. Example 3 — Insert in sorted order Use the position to insert a value.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 3, 5, 7};
auto it = std::lower_bound(v.begin(), v.end(), 4);
v.insert(it, 4);
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works lower_bound() returns the place where 4 belongs, before 5.v.insert(it, 4) puts the new value there.The vector is still sorted. Frequently Asked Questions What does std::lower_bound() do? In C++, std::lower_bound() finds the first element that is not less than a value in a sorted range. Include <algorithm> to use it.
Which header and C++ version do I need for std::lower_bound()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::lower_bound() return? It returns an iterator to the first element that is not less than the value, or the last iterator if there is none.
What is the difference between lower_bound() and upper_bound()? lower_bound() returns the first element not less than the value. upper_bound() returns the first element greater than the value.
Does lower_bound() require a sorted range? Yes. It uses binary search, which only works on sorted data.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful
Helpful Share Copy link Suggestion