C++ std::lower_bound() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
std::lower_bound(v.begin(), v.end(), 20);
std::lower_bound(v.begin(), v.end(), 20, comp);
CallWhat 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
C++
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

ParameterDescription
first, lastRequired. A sorted range.
valueRequired. The value to search for.
compOptional. The same comparison that was used to sort the range.

Return Value

Return ValueDescription
IteratorPoints at the first element not less than value.
lastReturned when every element is less than value.

C++ Examples

Example 1 — Find the first 20

Find the first element that is at least 20.

C++
#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

  1. The vector is sorted, so a binary search works.
  2. The first element that is not less than 20 is at index 1.
  3. 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.

C++
#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;
}

How It Works

  1. There is no 25, so the search returns the first larger element.
  2. That is 30 at index 3.
  3. Inserting at that position keeps the vector sorted.

Example 3 — Insert in sorted order

Use the position to insert a value.

C++
#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

  1. lower_bound() returns the place where 4 belongs, before 5.
  2. v.insert(it, 4) puts the new value there.
  3. The vector is still sorted.

Frequently Asked Questions

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.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the first element that is not less than the value, or the last iterator if there is none.
lower_bound() returns the first element not less than the value. upper_bound() returns the first element greater than the value.
Yes. It uses binary search, which only works on sorted data.

Next: make_heap()

Rearranges a range into a heap.

make_heap() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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