std::bsearch

From Cppreference

Jump to: navigation, search
Defined in header <cstdlib>

void *bsearch( const void *key, const void *ptr, size_t count, size_t size,
               int (*comp)(const void *, const void *) );

Finds an element equal to element pointed to by key in an array pointed to by ptr. The array contains count elements of size size. Function pointed to by comp is used for object comparison.

Contents

[edit] Parameters

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.

[edit] Return value

pointer to the found element or NULL otherwise.

[edit] Example

[edit] See also

qsort
sorts a range of elements with unspecified type
(function)
equal_range
returns range of elements matching a specific key
(function template)