Поделиться через


_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).

This function validates its parameters. 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 Parameter Validation. If execution is allowed to continue, errno is set to EINVAL and the function returns NULL.

Требования

Routine

Required header

_lfind

<search.h>

For more compatibility information, see Compatibility in the Introduction.

Пример

// 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" );
}

Hello found

Эквивалент в .NET Framework

System::Collections::ArrayList::Contains

См. также

Основные понятия

Searching and Sorting

_lfind_s

bsearch

_lsearch

qsort