_lfind_s

執行所指定索引鍵的線性搜尋。 版本 _lfind 具有 CRT 中的安全性功能中所述 的安全性增強功能。

語法

void *_lfind_s(
   const void *key,
   const void *base,
   unsigned int *num,
   size_t size,
   int (__cdecl *compare)(void *, const void *, const void *),
   void * context
);

參數

key
要搜尋的物件。

base
搜尋資料基底的指標。

number
陣列元素數目。

size
陣列元素的大小,以位元組為單位。

compare
比較常式的指標。 第一個參數是 context 指標。 第二個參數是搜尋索引鍵的指標。 第三個參數是要與索引鍵比較之陣列元素的指標。

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

傳回值

如果找到索引鍵,_lfind_s 將指標傳回至 basekey 相符的陣列元素。 如果找不到索引鍵, _lfind_s 則傳 NULL 回 。

如果將不正確參數傳遞至函式,則會叫用不正確參數處理常式,如參數驗證 中所述 。 若允許繼續執行, errno 會設為 EINVAL ,且函式會傳回 NULL中所述。

錯誤條件

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

備註

_lfind_s 函式會在 number 個元素的陣列中執行線性搜尋,尋找 key 值,每個元素 size 個位元組。 不同于 bsearch_s_lfind_s 不需要排序陣列。 base 引數是要搜尋之陣列基底的指標。 compare 引數是使用者所提供之常式的指標,該常式比較兩個陣列元素,然後傳回一個指定其關聯性的值。 _lfind_s 在搜尋時會呼叫 compare 常式一或多次,每次呼叫會將 context 指標傳遞至兩個陣列元素。 compare 常式必須比較項目,然後傳回非零 (表示項目不同) 或 0 (表示項目完全相同)。

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

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

需求

常式 必要的標頭
_lfind_s <search.h>

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

範例

// crt_lfind_s.cpp
// This program uses _lfind_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
// Codepage 850 is the OEM codepage used by the command line,
// so \x00e1 is the German Sharp S

char *array1[] = { "wei\x00e1", "weis", "annehmen", "weizen", "Zeit",
                   "weit" };

#define GERMAN_LOCALE "German_Germany.850"

#endif

#ifdef CODEPAGE_1252
   // If using codepage 1252 (ISO 8859-1, Latin-1), use \x00df
   // for the German Sharp S
char *array1[] = { "wei\x00df", "weis", "annehmen", "weizen", "Zeit",
                   "weit" };

#define GERMAN_LOCALE "German_Germany.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, const void *str1, const void *str2)
{
    char *s1 = *(char**)str1;
    char *s2 = *(char**)str2;

    locale& loc = *( reinterpret_cast< locale * > ( pvlocale));

    return use_facet< collate<char> >(loc).compare(
       s1, s1+strlen(s1),
       s2, s2+strlen(s2) );
}

void find_it( char *key, char *array[], unsigned int num, locale &loc )
{
   char **result = (char **)_lfind_s( &key, array,
                      &num, sizeof(char *), compare, &loc );
   if( result )
      printf( "%s found\n", *result );
   else
      printf( "%s not found\n", key );
}

int main( )
{
   find_it( "weit", array1, sizeof(array1)/sizeof(char*), locale(GERMAN_LOCALE) );
}
weit found

另請參閱

搜尋和排序
bsearch_s
_lsearch_s
qsort_s
_lfind