Share via


_lfind_s

更新 : 2007 年 11 月

指定されたキーで線形検索を実行します。この関数は、「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
    検索するデータ配列のベースへのポインタ。

  • num
    配列要素の数。

  • size
    配列要素のサイズ (バイト単位)。

  • compare
    比較ルーチンへのポインタ。最初のパラメータは context ポインタです。2 番目のパラメータは、検索用のキーへのポインタです。3 番目のパラメータは、キーと比較される配列要素へのポインタです。

  • context
    比較関数の中でアクセスされる可能性のあるオブジェクトへのポインタ。

戻り値

キーが見つかった場合、_lfind_s は、key に一致する base の指す配列要素へのポインタを返します。キーが見つからなかった場合は、_lfind_s が NULL を返します。

この関数に無効なパラメータが渡されると、「パラメータの検証」に説明されているように、無効なパラメータ ハンドラが呼び出されます。実行の継続が許可された場合、errno が EINVAL に設定され、関数から NULL が返されます。

エラー条件

Key

base

compare

num

size

errno

NULL

any

any

any

any

EINVAL

any

NULL

any

!= 0

any

EINVAL

any

any

any

any

0

EINVAL

any

any

NULL

an

any

EINVAL

解説

_lfind_s 関数は、width のバイト数ごとに、num 要素の配列で key 値のリニア サーチを実行します。bsearch_s とは異なり、_lfind_s では、配列を並べ替える必要がありません。base 引数は、検索する配列のベースへのポインタです。compare 引数は、2 つの配列要素を比較し、両者の関係を指定する値を返すユーザー指定ルーチンへのポインタです。_lfind_s は、並べ替えの間に 1 回以上 compare ルーチンを呼び出し、そのたびに context ポインタと 2 つの配列要素へのポインタを渡します。compare ルーチンは必ず要素を比較し、0 以外の値 (2 つの要素が異なることを示す場合) または 0 (2 つの要素が同じであることを示す場合) を返します。

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

必要条件

ルーチン

必須ヘッダー

_lfind_s

<search.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

使用例

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

.NET Framework の相当するアイテム

Contains

参照

参照

検索と並べ替え

bsearch_s

_lsearch_s

qsort_s

_lfind