_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
    搜尋資料的基底指標。

  • num
    陣列的元素數目。

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

  • compare
    指標比較常式。第一個參數是context指標。第二個參數是變數的指標,搜尋的索引鍵。第三個參數是要比較具有索引鍵的陣列項目的指標。

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

傳回值

如果找到索引鍵, _lfind_s的陣列元素中傳回的指標base符合key。如果找不到機碼, _lfind_s會傳回NULL。

如果不正確的參數傳遞給函式,不正確的參數處理常式會叫用,如所述參數驗證。如果要繼續,請允許執行errno設定為 [ EINVAL ,則函數會傳回NULL。

錯誤狀況

索引鍵

base

compare

num

size

errno

NULL

任何

任何

任何

任何

EINVAL

任何

NULL

任何

!= 0

任何

EINVAL

任何

任何

任何

任何

EINVAL

任何

任何

NULL

系統

任何

EINVAL

備註

_lfind_s函式會執行線性搜尋值key的陣列中num項目,每個width個位元組。不像bsearch_s, _lfind_s並不需要排序陣列。base引數是陣列,要搜尋的基底指標。compare引數為使用者提供的常式會比較兩個陣列項目,則會傳回值,指定兩者的關聯性的指標。_lfind_s呼叫compare日常的一或多次傳遞搜尋期間context指標,並在每個呼叫上的兩個陣列元素的指標。compare常式必須比較項目,然後傳回非零值 (表示項目會不同) 或 0 (也就相同的項目)。

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

需求

常式

所需的標頭

_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) );
}
  

.NET Framework 對等用法

Contains

請參閱

參考

搜尋和排序

bsearch_s

_lsearch_s

qsort_s

_lfind