C++ std::binary_search() Function

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

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.

C++
std::binary_search(v.begin(), v.end(), 6);
std::binary_search(v.begin(), v.end(), 5, std::greater<int>());
CallWhat 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
C++
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

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

Return Value

Return ValueDescription
trueAn element equal to the value exists.
falseNo element is equal to the value.

C++ Examples

Example 1 — Value is present

Search a sorted vector for 6.

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

  1. The vector is sorted, so a binary search is allowed.
  2. The search looks at the middle element, 6, and finds it.
  3. The result is true.

Example 2 — Value is missing

Search for a value that is not there.

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

  1. The search checks the middle, then narrows to 8 and 10.
  2. Between 6 and 8 there is no 7.
  3. The search area becomes empty, and the result is false.

Example 3 — Descending data

Pass the same comparison that was used to sort.

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

  1. The vector is sorted from large to small.
  2. std::greater<int>() tells the search about that order.
  3. 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.

Next: clamp()

Limits a value so it stays between a low and a high bound.

clamp() 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.

13 people found this page helpful