C++ std::binary_search() Function

Definition
In C++, std::binary_search() checks if a value is present in a sorted range. It returns true or false.
It halves the search area at every step, so it is very fast on large sorted data. It does not tell you where the value is. Use lower_bound() for that.
Syntax
Add #include <algorithm>, then call std::binary_search(). Start with these two lines.
std::binary_search(v.begin(), v.end(), 6);
std::binary_search(v.begin(), v.end(), 5, std::greater<int>());
| Call | What it does |
|---|
std::binary_search(v.begin(), v.end(), 6); | Is 6 in the sorted vector? |
std::binary_search(v.begin(), v.end(), 5, std::greater<int>()); | Search data sorted in descending order. |
Library declarations
template<class ForwardIt, class T>
bool binary_search(ForwardIt first, ForwardIt last, const T& value);
template<class ForwardIt, class T, class Compare>
bool binary_search(ForwardIt first, ForwardIt last,
const T& value, Compare comp);
Parameters
| Parameter | Description |
|---|
| first, last | Required. A sorted range. |
| value | Required. The value to look for. |
| comp | Optional. The comparison that was used to sort the range. |
Return Value
| Return Value | Description |
|---|
true | An element equal to the value exists. |
false | No element is equal to the value. |
C++ Examples
Example 1 — Value is present
Search a sorted vector for 6.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {2, 4, 6, 8, 10};
bool found = std::binary_search(v.begin(), v.end(), 6);
std::cout << (found ? "Found" : "Not found") << '\n';
return 0;
}
How It Works
- The vector is sorted, so a binary search is allowed.
- The search looks at the middle element, 6, and finds it.
- The result is
true.
Example 2 — Value is missing
Search for a value that is not there.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {2, 4, 6, 8, 10};
bool found = std::binary_search(v.begin(), v.end(), 7);
std::cout << std::boolalpha << "Found 7? " << found << '\n';
return 0;
}
How It Works
- The search checks the middle, then narrows to 8 and 10.
- Between 6 and 8 there is no 7.
- The search area becomes empty, and the result is
false.
Example 3 — Descending data
Pass the same comparison that was used to sort.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {9, 7, 5, 3};
bool found = std::binary_search(v.begin(), v.end(), 5, std::greater<int>());
std::cout << std::boolalpha << "Found 5? " << found << '\n';
return 0;
}
How It Works
- The vector is sorted from large to small.
std::greater<int>() tells the search about that order.- With the right order, 5 is found.
Frequently Asked Questions
In C++, std::binary_search() checks whether a value exists 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 true if the sorted range contains an element equal to the value, and false otherwise.
binary_search() only returns true or false. Use std::lower_bound() to get an iterator.
The result is not reliable. Sort the data first.
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.
13 people found this page helpful