
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Binary Search Functions in C++ STL: Lower Bound and Upper Bound
Binary search is the fastest algorithm for finding an item from a sorted list of items. C++ STL provides built-in functions to perform binary search operations on sorted ranges of elements. In this article, we will learn binary_search, lower_bound and upper_bound functions in C++ STL. First of all, let's understand what binary search is.
What is Binary Search?
Binary search is a fast search algorithm with run-time complexity of (log n). This search algorithm works on the principle of divide and conquer, since it divides the array into half before searching. This algorithm works only on sorted collection of elements.
Binary search looks for a particular key value by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. But if the middle item has a value greater than the key value, the right sub-array of the middle item is searched. Otherwise, the left sub-array is searched. This process continues recursively until the size of a subarray reduces to zero.

Binary Search Functions in C++ STL
There are 3 types of binary search functions in C++ STL:
The binary_search() Function
The std::binary_search() function is used to check whether an element exists in the sorted range or not. It returns true if the element is found, otherwise it returns false.
Example
In the code below, the std::binary_search() function is applied to a vector of integers to check if number 30 exists in the vector:
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> v = {10, 20, 30, 40, 50}; if (binary_search(v.begin(), v.end(), 30)) cout << "Element found" << endl; else cout << "Element not found" << endl; return 0; }
The output of the above code will be:
Element found
The lower_bound() Function
The std::lower_bound() is a special type of binary search function that returns a pointer pointing to the first element in the sorted range that is not less than (i.e., greater than or equal to) the given value.
For example, if you search for the lower bound of 30 in a sorted vector, it will return an iterator pointing to the first element that is greater than or equal to 30. Which means, even if you are searching for a value that does not exist in the vector, it will return the next greater element.
Example
In the code below, the std::lower_bound() function is applied to a vector of integers to find the lower bound of number 35:
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> v = {10, 20, 30, 40, 50}; auto it = lower_bound(v.begin(), v.end(), 35); if (it != v.end()) cout << "Lower bound of 35 is " << *it << endl; else cout << "Element not found" << endl; return 0; }
The output of the above code will be:
Lower bound of 35 is 40
The upper_bound() Function
The std::upper_bound() function is a type of binary search function that returns a pointer pointing to the first element in the sorted range that is greater than the given value.
For example, if you search for the upper bound of 30 in a sorted vector, it will return an iterator pointing to the first element that is greater than 30. If there is no such element, it will return an iterator pointing to the end of the range.
Example
In the code below, the std::upper_bound() function is applied to a vector of integers to find the upper bound of number 30:
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> v = {10, 20, 30, 40, 50}; auto it = upper_bound(v.begin(), v.end(), 30); if (it != v.end()) cout << "Upper bound of 30 is " << *it << endl; else cout << "Element not found" << endl; return 0; }
The output of the above code will be:
Upper bound of 30 is 40