头文件<stdlib.h> <cstdlib>
1. 函数原型
void * bsearch(const void * key, const void * base, size_t num, size_t size, int (*comparator) ( const void *, const void * ) );
2. 函数说明
Binary search in array:
Searches the given key in the array pointed by base that is formed by num elements, each of a size of size bytes, and returns a void* pointer to the first entry in the table that matches the search key.
To perform the search, the function compares the elements to the key by calling the function comparator specified as the last argument.
Because this function performs a binary search, the values in the base array are expected to be already sorted in ascending order, with the same criteria used by comparator.
3. 函数参数
key
Pointer to the object that serves as key for the search, type-casted as a void*.
base
Pointer to the first object of the array where the search is performed, type-casted as a void*.
num
Number of elements in the array pointed by base.
size
Size in bytes of each element in the array.
comparator
Function that compares two elements. The function shall follow this prototype:
int comparator ( const void * pkey, const void * pelem );
The function must accept two parameters: the first one pointing to the key object, and the second one to an
element of the array, both type-casted as void*. The function should cast the parameters back to some data
type and compare them.
The return value of this function should represent whether the value pointed by pkey is considered less than,
equal, or grater than the value pointed by pelem by returning, respectively, a negative value, zero or a positive
value.
For types that support regular comparison operators, a general comparator function may look like:
int compareMyType (const void * a, const void * b)
{
if ( *(MyType*)a > *(MyType*)b ) return 1;
if ( *(MyType*)a == *(MyType*)b ) return 0;
if ( *(MyType*)a < *(MyType*)b ) return -1;
}
4. 返回类型
A pointer to an entry in the array that matches the search key.
If key is not found, a NULL pointer is returned.
5. 示例
/* bsearch example */
#include <stdio.h>
#include <stdlib.h>
int compareints (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int values[] = { 10, 20, 25, 40, 90, 100 };
int main ()
{
int * pItem;
int key = 40;
pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
if (pItem!=NULL)
printf ("%d is in the array.\n",*pItem);
else
printf ("%d is not in the array.\n",key);
return 0;
}
In the example there is an array of sorted int values. There is also a function called compare that compares the values pointed by the two parameters as if they were pointers to int values (which they indeed are) and returns the result of the subtraction of the values pointed by both, which gives 0 as result if they are equal, a positive result if the value pointed by a is greater than the pointed by b or a negative result if the value pointed by b is greater.
In the main, function there is a call to bsearch with 40 as key, so the function finds that key in the array of values and the program prints out:
For C strings, strcmp can directly be used as the last argument for bsearch:
/* bsearch example with strings */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char strvalues[][20] = {"some","example","strings","here"};
int main ()
{
char * pItem;
char key[20] = "example";
/* sort elements in array: */
qsort (strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);
/* search for the key: */
pItem = (char*) bsearch (key, strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);
if (pItem!=NULL)
printf ("%s is in the array.\n",pItem);
else
printf ("%s is not in the array.\n",key);
return 0;
}
输出: