Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)

Last Updated : 4 Jun, 2026

Binary search functions in C++ STL provide an efficient way to search and locate elements in sorted containers. These functions use the binary search algorithm internally and perform operations in O(log n) time complexity.

  • Used for fast searching in sorted ranges.
  • Require the data to be sorted before use.
  • Help find the presence or position of elements.

Common Binary Search Functions in STL

  • binary_search(): Checks whether a specified element exists in a sorted range.
  • lower_bound(): Returns an iterator to the first element that is greater than or equal to the given value.
  • upper_bound(): Returns an iterator to the first element that is strictly greater than the given value.
  • equal_range(): Returns both lower and upper bounds as a pair of iterators.

The std::binary_search() function is used to find an element in the container. It will only work on the sorted data. It will take logarithmic time to search an element from the container as it implementing the binary search algorithm internally.

Syntax:

binary_search(start, end, val);

Example: Program to show how to use the binary_search() for searching an element in the given range

C++
#include <bits/stdc++.h>
using namespace std;

// Function for check an element whether it
// is present or not
void isPresent(vector<int> &arr, int val) {
  
    // using binary_search to check if val exists
    if (binary_search(arr.begin(), arr.end(), val))
        cout << val << " exists in vector";
    else
        cout << val << " does not exist";

    cout << endl;
}

int main() {
    vector<int> arr = {10, 15, 20, 25, 30, 35};

    int val1 = 15;
    int val2 = 23;

    isPresent(arr, val1);
    isPresent(arr, val2);

    return 0;
}

Output
15 exists in vector
23 does not exist

Explanation

  • The vector arr contains sorted elements {10, 15, 20, 25, 30, 35}.
  • The isPresent() function uses std::binary_search() to check whether a given value exists in the vector.
  • binary_search(arr.begin(), arr.end(), val) returns true if val is found in the specified range and false otherwise.
  • For val1 = 15, the function finds the element and prints "15 exists in vector".
  • For val2 = 23, the element is not present in the vector, so the function prints "23 does not exist".
  • Since binary_search() works on sorted data and uses the binary search algorithm internally, the search operation takes O(log n) time.

Time Complexity: O(log n), where n is the number of elements.
Auxiliary Space: O(1)

lower_bound()

The std::lower_bound function returns an iterator to the first element in a sorted range that is greater than or equal to the specified value. It is commonly used to find the position of an element or the correct insertion point while maintaining sorted order.

Syntax:

lower_bound(start, end, val);

C++
#include <bits/stdc++.h>
using namespace std;

int main() {

    vector<int> arr1 = {10, 15, 20, 25, 30, 35};
    vector<int> arr2 = {10, 15, 20, 20, 25, 30, 35};
    vector<int> arr3 = {10, 15, 25, 30, 35};
  
    int val = 20;

    // using lower_bound() to check if val exists
    // in arr1, single occurrence, prints 2
    cout << lower_bound(arr1.begin(), arr1.end(), val)
      - arr1.begin();
    cout << endl;

    // using lower_bound() to check if val exists
    // in arr2, multiple occurrence , prints 2
    cout << lower_bound(arr2.begin(), arr2.end(), val)
      - arr2.begin();
    cout << endl;

    // using lower_bound() to check if val exists
    // in arr3, no occurrence , prints 2 
    // ( index of next higher)
    cout << lower_bound(arr3.begin(), arr3.end(), val)
      - arr3.begin();
    cout << endl;
  return 0;
}

Output
2
2
2

Explanation

  • In arr1, the value 20 occurs once, so lower_bound() returns the index of 20, which is 2.
  • In arr2, the value 20 appears multiple times, and lower_bound() returns the index of its first occurrence, which is 2.
  • In arr3, the value 20 is not present. The function returns the index of the first element greater than 20, which is 25 at index 2.
  • lower_bound() is useful for finding insertion positions in sorted containers without breaking the sorted order.

Time Complexity: O(log n), Where n is the number of elements.
Auxiliary Space: O(1)

upper_bound()

The std::upper_bound function returns an iterator to the first element in a sorted range that is strictly greater than the specified value. It is commonly used to find the position immediately after the last occurrence of a value in a sorted container.

Syntax:

upper_bound(start, end, val);

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> arr1 = {10, 15, 20, 25, 30, 35};
    vector<int> arr2 = {10, 15, 20, 20, 25, 30, 35};
    vector<int> arr3 = {10, 15, 25, 30, 35};
  
    int val = 20;

    // using upper_bound() to check if val exists
    // in arr1, single occurrence, prints 3
    cout << upper_bound(arr1.begin(), arr1.end(), val)
      - arr1.begin();
    cout << endl;

    // using upper_bound() to check if val exists
    // in arr2, multiple occurrence, prints 4
    cout << upper_bound(arr2.begin(), arr2.end(), val)
      - arr2.begin();
    cout << endl;

    // using upper_bound() to check if val exists
    // in arr3, no occurrence,  prints 2 
    // ( index of next higher)
    cout << upper_bound(arr3.begin(), arr3.end(), val)
      - arr3.begin();
    cout << endl;
  return 0;
}

Output
3
4
2

Explanation

  • In arr1, the first element greater than 20 is 25, which is at index 3.
  • In arr2, the value 20 appears multiple times. upper_bound() returns the position immediately after the last occurrence of 20, which is index 4.
  • In arr3, the value 20 is not present. The function returns the index of the first element greater than 20, which is 25 at index 2.
  • upper_bound() is often used along with lower_bound() to find the range or frequency of an element in a sorted container.

Time Complexity: O(log n), Where n is the number of element
Auxiliary Space: O(1)

Comment