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

任何

任何

任何

任何

EINVAL

任何

NULL

任何

!= 0

任何

EINVAL

任何

任何

任何

任何

= 0

EINVAL

任何

任何

NULL

系統

任何

EINVAL

備註

bsearch_s函式在執行已排序陣列的二進位搜尋num項目,每個width個位元組的大小。base值是要進行搜尋的陣列的基底指標和key所示的值。compare參數是指標,對使用者提供的常式,比較要求的索引鍵陣列元素,並且傳回下列值將用以指定兩者的關聯性的其中一個:

傳回值compare常式

描述

< 0

索引鍵是早於陣列元素。

0

索引鍵等於陣列元素。

> 0

索引鍵大於陣列元素。

context指標可能會很有用,如果搜尋的資料結構的部分物件,則比較功能需要存取物件的成員。compare函式可能會將 void 指標轉換成適當的物件型別和存取成員,該物件。額外的context參數會讓bsearch_s更安全,因為其他的內容可能會被用來避免使用靜態變數時,讓資料可以使用相關聯的重新進入錯誤compare函式。

需求

常式

所需的標頭

bsearch_s

<stdlib.h> 和 <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" );
}
  

.NET Framework 對等用法

BinarySearch

請參閱

參考

搜尋和排序

_lfind

_lsearch

qsort