Defined in header <cstdlib>
void* bsearch( const void* key, const void* ptr, std::size_t count,
               std::size_t size, /*compare-pred*/* comp );
void* bsearch( const void* key, const void* ptr, std::size_t count,
               std::size_t size, /*c-compare-pred*/* comp );
extern "C++" using /*compare-pred*/ = int(const void*, const void*); // exposition-only
extern "C" using /*c-compare-pred*/ = int(const void *, const void *); // exposition-only

Finds an element equal to element pointed to by key in an array pointed to by ptr. The array contains count elements of size bytes each and must be partitioned with respect to the object pointed to by key, that is, all the elements that compare less than must appear before all the elements that compare equal to, and those must appear before all the elements that compare greater than the key object. A fully sorted array satisfies these requirements. The elements are compared using function pointed to by comp.

The behavior is undefined if the array is not already partitioned in ascending order with respect to key, according to the same criterion that comp uses.

If the array contains several elements that comp would indicate as equal to the element searched for, then it is unspecified which element the function will return as the result.


key - pointer to the element to search for
ptr - pointer to the array to examine
count - number of element in the array
size - size of each element in the array in bytes
comp - comparison function which returns ​a negative integer value if the first argument is less than the second,

a positive integer value if the first argument is greater than the second and zero if the arguments are equal. key is passed as the first argument, an element from the array as the second.
The signature of the comparison function should be equivalent to the following:

int cmp(const void *a, const void *b);

The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.

Return value

Pointer to the found element or null pointer if the element has not been found.


Despite the name, neither C nor POSIX standards require this function to be implemented using binary search or make any complexity guarantees.

The two overloads provided by the C++ standard library are distinct because the types of the parameter comp are distinct (language linkage is part of its type).


#include <cstdlib>
#include <iostream>
int compare(const void *ap, const void *bp)
    const int *a = (int *) ap;
    const int *b = (int *) bp;
    if(*a < *b)
        return -1;
    else if(*a > *b)
        return 1;
        return 0;
int main(int argc, char **argv)
    const int ARR_SIZE = 8;
    int arr[ARR_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int key1 = 4;
    int *p1 = (int *) std::bsearch(&key1, arr, ARR_SIZE, sizeof(arr[0]), compare);
        std::cout << "value " << key1 << " found at position " << (p1 - arr) << '\n';
        std::cout << "value " << key1 << " not found\n";
    int key2 = 9;
    int *p2 = (int *) std::bsearch(&key2, arr, ARR_SIZE, sizeof(arr[0]), compare);
        std::cout << "value " << key2 << " found at position " << (p2 - arr) << '\n';
        std::cout << "value " << key2 << " not found\n";


value 4 found at position 3
value 9 not found

See also

sorts a range of elements with unspecified type
returns range of elements matching a specific key
(function template)
C documentation for bsearch

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.