bsearch_s
执行一个排序数组的二进制搜索。 bsearch 的一些版本提供安全增强功能(如 CRT 中的安全功能所述)。
void *bsearch_s(
const void *key,
const void *base,
size_t num,
size_t width,
int ( __cdecl *compare ) ( void *, const void *key, const void *datum),
void * context
);
参数
key
要搜索的对象。base
要搜索数据的基本指针。num
元素的数目width
元素的宽度。compare
比较两元素的回调函数。 第一个参数是 context 指针。 第二个参数是指向将要搜索的 key。 第三个参数是指向数组元素与 key进行比较。context
在比较函数中可能访问的对象的指针。
返回值
bsearch_s 返回指向key 数组中出现的指向的 base。 如果不存在 key,则函数返回 NULL。 如果数组不按升序排序顺序或者不包含带相同键的复制记录,结果是不可预知的。
如果传递给函数的是无效参数,则调用无效参数处理程序,正如 参数验证 所述。 如果允许继续执行, errno设置为 EINVAL 并且函数返回 NULL。 有关更多信息,请参见 errno、_doserrno、_sys_errlist 和 _sys_nerr。
错误情况
key |
base |
compare |
num |
width |
errno |
NULL |
any |
any |
any |
any |
EINVAL |
any |
NULL |
any |
!= 0 |
any |
EINVAL |
any |
any |
any |
any |
= 0 |
EINVAL |
any |
any |
NULL |
an |
any |
EINVAL |
备注
bsearch_s 函数在数组大小执行顺序的 num 元素,每一个二进制搜索 width 字节。 base值是指向将要搜索数组的基,key 是查找的值。 compare 参数是指向比较请求的键处到数组并返回指定元素及其关系的下列值之一的用户提供的例程:
compare 例程将返回的值 |
说明 |
---|---|
< 0 |
键小于数组元素。 |
0 |
键与数组元素相等。 |
> 0 |
键大于数组元素。 |
如果搜索的数据结构是对象的一部分, 函数需要访问对象的成员,context 指针可能有用。 compare 函数可以转换无效指针到适当的对象类型,并且访问该对象的成员。 添加context参数使得bsearch_s更安全,因为附加context可以被用来避免与是使用静态变量关联的重入错误,以使compare函数的数据可用。
要求
例程 |
必需的标头 |
---|---|
bsearch_s |
<stdlib.h> and <search.h> |
有关其他兼容性详细信息,请参阅简介中的 兼容性 。
示例
此应用程序使用 qsort_s的字符串数组,然后使用 bsearch_s 查找单词“猫”。
// crt_bsearch_s.cpp
// This program uses bsearch_s to search a string array,
// passing a locale as the context.
// compile with: /EHsc
#include <stdlib.h>
#include <stdio.h>
#include <search.h>
#include <process.h>
#include <locale.h>
#include <locale>
#include <windows.h>
using namespace std;
// The sort order is dependent on the code page. Use 'chcp' at the
// command line to change the codepage. When executing this application,
// the command prompt codepage must match the codepage used here:
#define CODEPAGE_850
#ifdef CODEPAGE_850
#define ENGLISH_LOCALE "English_US.850"
#endif
#ifdef CODEPAGE_1252
#define ENGLISH_LOCALE "English_US.1252"
#endif
// The context parameter lets you create a more generic compare.
// Without this parameter, you would have stored the locale in a
// static variable, thus making it vulnerable to thread conflicts
// (if this were a multithreaded program).
int compare( void *pvlocale, char **str1, char **str2)
{
char *s1 = *str1;
char *s2 = *str2;
locale& loc = *( reinterpret_cast< locale * > ( pvlocale));
return use_facet< collate<char> >(loc).compare(
s1, s1+strlen(s1),
s2, s2+strlen(s2) );
}
int main( void )
{
char *arr[] = {"dog", "pig", "horse", "cat", "human", "rat", "cow", "goat"};
char *key = "cat";
char **result;
int i;
/* Sort using Quicksort algorithm: */
qsort_s( arr,
sizeof(arr)/sizeof(arr[0]),
sizeof( char * ),
(int (*)(void*, const void*, const void*))compare,
&locale(ENGLISH_LOCALE) );
for( i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i ) /* Output sorted list */
printf( "%s ", arr[i] );
/* Find the word "cat" using a binary search algorithm: */
result = (char **)bsearch_s( &key,
arr,
sizeof(arr)/sizeof(arr[0]),
sizeof( char * ),
(int (*)(void*, const void*, const void*))compare,
&locale(ENGLISH_LOCALE) );
if( result )
printf( "\n%s found at %Fp\n", *result, result );
else
printf( "\nCat not found!\n" );
}