_lfind

Performs a linear search for the specified key. A more secure version of this function is available; see _lfind_s.

void *_lfind(
   const void *key,
   const void *base,
   unsigned int *num,
   unsigned int width,
   int (__cdecl *compare)(const void *, const void *)
);

参数

  • key
    Object to search for.

  • base
    Pointer to the base of search data.

  • num
    Number of array elements.

  • width
    Width of array elements.

  • compare
    Pointer to comparison routine. The first parameter is a pointer to key for search. The second parameter is a pointer to array element to be compared with key.

返回值

If the key is found, _lfind returns a pointer to the element of the array at base that matches key. If the key is not found, _lfind returns NULL.

备注

The _lfind function performs a linear search for the value key in an array of num elements, each of width bytes. Unlike bsearch, _lfind does not require the array to be sorted. The base argument is a pointer to the base of the array to be searched. The compare argument is a pointer to a user-supplied routine that compares two array elements and then returns a value specifying their relationship. _lfind calls the compare routine one or more times during the search, passing pointers to two array elements on each call. The compare routine must compare the elements and then return nonzero (meaning the elements are different) or 0 (meaning the elements are identical).

此函数验证其参数。 If compare, key or num is NULL, or if base is NULL and *num is nonzero, or if width is less than zero, the invalid parameter handler is invoked, as described in 参数验证. 如果允许执行继续,errno设置为EINVAL,并且函数返回NULL。

要求

例程

必需的标头

_lfind

<search.h>

有关更多兼容性信息,请参见“简介”中的兼容性

示例

// crt_lfind.c
// This program uses _lfind to search a string array
// for an occurrence of "hello".

#include <search.h>
#include <string.h>
#include <stdio.h>

int compare(const void *arg1, const void *arg2 )
{
   return( _stricmp( * (char**)arg1, * (char**)arg2 ) );
}

int main( )
{
   char *arr[] = {"Hi", "Hello", "Bye"};
   int n = sizeof(arr) / sizeof(char*);
   char **result;
   char *key = "hello";

   result = (char **)_lfind( &key, arr, 
                      &n, sizeof(char *), compare );

   if( result )
      printf( "%s found\n", *result );
   else
      printf( "hello not found!\n" );
}
  

.NET Framework 等效项

System::Collections::ArrayList::Contains

请参见

参考

搜索和排序

_lfind_s

bsearch

_lsearch

qsort