_lfind_s

指定されたキーの線形探索を実行します。 CRT_lfindセキュリティ機能の説明に従ってセキュリティが強化されたバージョン。

構文

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 ポインターです。 2 番目のパラメーターは、検索用のキーへのポインターです。 3 番目のパラメーターは、キーと比較する配列要素へのポインターです。

context
比較関数内でアクセスされることのあるオブジェクトへのポインター。

戻り値

キーが見つかった場合、_lfind_skey と一致する base の配列要素のポインターを返します。 キーが見つからない場合は、 _lfind_sNULL

無効なパラメーターが関数に渡された場合、「パラメーターの検証」で説明されているように、無効なパラメーター ハンドラーが呼び出されます。 実行の継続が許可された場合、 errnoEINVAL に設定され、関数が NULLのセキュリティが強化されたバージョンです。

エラー条件

key base compare number size errno
NULL any any any any EINVAL
any NULL any != 0 any EINVAL
any any any any ゼロ EINVAL
any any NULL 1 つ any EINVAL

解説

_lfind_s 関数は、number 要素の配列の値 key に対する一方向の検索を、size バイトごとに実行します。 とは異なり bsearch_s_lfind_s 配列を並べ替える必要はありません。 base 引数は、検索する配列のベースへのポインターです。 compare 引数は、2 つの配列要素を比較して、それらの関係を指定する値を返すユーザー指定のルーチンへのポインターです。 _lfind_s は検索中に compare ルーチンを 1 回以上呼び出し、各呼び出しにおいて context ポインターと 2 つの配列要素へのポインターを渡します。 compare ルーチンは要素を比較し、ゼロ以外 (要素が異なる場合) または 0 (要素が同じ場合) を返す必要があります。

_lfind_s_lfind と似ていますが、比較関数の引数と関数のパラメーター リストへ context ポインターが追加されている点が異なります。 context ポインターは、検索対象のデータ構造体がオブジェクトの一部であり、compare 関数でオブジェクトのメンバーにアクセスする必要がある場合に役立ちます。 compare 関数は void ポインターを該当するオブジェクト型にキャストして、そのオブジェクトのメンバーにアクセスできます。 パラメーターを context 追加すると _lfind_s 、追加のコンテキストを使用して、静的変数を使用して関数でデータを使用できるようにするために関連する再入バグを回避できるため、セキュリティが compare 強化されます。

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

必要条件

ルーチンによって返される値 必須ヘッダー
_lfind_s <search.h>

互換性の詳細については、「 Compatibility」を参照してください。

// 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