bsearch_s

對已排序陣列執行二進位搜尋。 此函式是 的版本 bsearch ,具有 CRT 中安全性功能中所述 的安全性增強功能。

語法

void *bsearch_s(
   const void *key,
   const void *base,
   size_t number,
   size_t width,
   int ( __cdecl *compare ) ( void *, const void *key, const void *datum),
   void * context
);

參數

key
要搜尋之索引鍵的指標。

base
搜尋資料基底的指標。

number
項目數。

width
項目的寬度。

compare
比較兩個項目的回呼函式。 第一個引數是 context 指標。 第二個引數是搜尋用的 key 指標。 第三個引數是要與 key比較之陣列項目的指標。

context
可以在比較函式中存取的物件指標。

傳回值

bsearch_s 會傳回 key 所指陣列中, base的指標 。 如果 key 找不到 ,函式會傳 NULL 回 。 如果陣列不是以遞增排序次序,或包含具有相同索引鍵的重複記錄,則結果無法預測。

如果將不正確參數傳遞至函式,它會叫用不正確參數處理常式,如參數驗證 中所述 。 若允許繼續執行, errno 會設為 EINVAL ,且函式會傳回 NULL中所述。 如需詳細資訊,請參閱errno, _doserrno, _sys_errlist_sys_nerr.

錯誤條件

key base compare number width errno
NULL 任意 任意 任意 任意 EINVAL
任意 NULL 任意 != 0 任意 EINVAL
任意 任意 任意 任意 = 0 EINVAL
任意 任意 NULL an 任意 EINVAL

備註

bsearch_s 函式會執行 number 項目已排序陣列的二進位搜尋,每個的大小為 width 個位元組。 base 值是要搜尋之陣列的基底指標, key 是要搜尋的值。 compare 參數是使用者提供的常式指標,這個常式會比較要求的索引鍵與陣列項目,並傳回下列其中一個指定其關聯性的值:

compare 常式傳回的值 描述
< 0 索引鍵小於陣列項目。
0 索引鍵等於陣列項目。
> 0 索引鍵大於陣列項目。

context 指標適用於搜尋的資料結構是物件的一部分,且比較函式需要存取物件成員的情況。 compare 函式可能會將 void 指標轉換成適當的物件類型,並存取該物件的成員。 新增 context 參數會更安全 bsearch_s ,因為內容可用來避免與使用靜態變數相關聯的重新進入錯誤,讓函式可以使用 compare 資料。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭
bsearch_s <stdlib.h > 和 < search.h>

如需相容性詳細資訊,請參閱相容性

範例

此程式會使用 qsort_s 排序字串陣列,然後使用 bsearch_s 來尋找 「cat」 這個字。

// 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" );
}
cat cow dog goat horse human pig rat
cat found at 002F0F04

另請參閱

搜尋和排序
_lfind
_lsearch
qsort