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

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.

Common Binary Search Functions in STL

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 &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 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

**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; }

`

**Explanation

**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 arr1 = {10, 15, 20, 25, 30, 35}; vector arr2 = {10, 15, 20, 20, 25, 30, 35}; vector 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; }

`

**Explanation

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