lfind - lsearch - Find Key in Array
-
#include <search.h>
char *lfind(char *search_key, char *base,
unsigned int *num, unsigned int *width,
int (*compare)(const void *key, const void *element));
char *lsearch(char *search_key, char *base,
unsigned int *num, unsigned int *width,
int (*compare)(const void *key, const void *element));
-
key and element are different.
key and element are identical.
lfind and lsearch perform a linear search for the value search_key in an array of num elements, each of width bytes in size. Unlike bsearch, lsearch and lfind do not require that you sort the array first. The argument base points to the base of the array to be searched.
If lsearch does not find the search_key, it adds the search_key to the end of the array and increments num by one. If lfind does not find the search_key, it does not add the search_key to the array.
The compare argument is a pointer to a function you must supply that takes a pointer to the key argument and to an array element, in that order. Both lfind and lsearch call this function one or more times during the search. The function must compare the key and the element and return one of the following values:
Value
Nonzero0
Note: In earlier releases of C Set ++, lfind and lsearch began with an underscore (_lfind and _lsearch). Because they are defined by the X/Open standard, the underscore has been removed. For compatibility, The Developer's Toolkit will map _lfind and _lsearch to lfind and lsearch for you.
If search_key is found, both lsearch and lfind return a pointer to that element of the array to which base points. If search_key is not found, lsearch returns a pointer to a newly added item at the end of the array, while lfind returns NULL.
This example uses lfind to search for the keyword PATH in the command-line arguments.
#include <search.h>#include <string.h>
#include <stdio.h>
#define CNT 2
int compare(const void *arg1,const void *arg2)
{
return (strncmp(*(char **)arg1, *(char **)arg2, strlen(*(char **)arg1)));
}
int main(void)
{
char **result;
char *key = "PATH";
unsigned int num = CNT;
char *string[CNT] = {
"PATH = d:\\david\\matthew\\heather\\ed\\simon","LIB = PATH\\abc" };
/* The following statement finds the argument that starts with "PATH" */
if ((result = (char **)lfind((char *)&key, (char *)string, &num,
sizeof(char *), compare)) != NULL)
printf("%s found\n", *result);
else
printf("PATH not found \n");
return 0;
/****************************************************************************
The output should be:
PATH = d:\david\matthew\heather\ed\simon found
****************************************************************************/
}
Syntax
Description
Returns
Example Code
分享到:
相关推荐
- **`lsearch`(线性搜索)**:用于在数组中查找特定元素。 - **`lfind`(线性搜索)**:用于在数组中查找特定元素。 #### 其他相关知识点 - **什么是标准预定义宏?** - 标准预定义宏是编译器自动定义的宏,用于...
- **排序和搜索函数**:`qsort()` 对数组进行排序,`bsearch()` 二分搜索,`lsearch()` 和 `lfind()` 线性搜索。 - **随机数生成**:`srand()` 设置随机数种子,`rand()` 生成随机数。 #### 其他相关概念 - **...
- `bsearch()`, `lfind()`, `lsearch()`: 在排序或未排序数组中搜索元素。 - `qsort()`: 对数组进行快速排序。 - `rand()`, `srand()`: 生成随机数。 7. **文件操作**: - `close()`, `creat()`, `dup()`, `dup...
* qsort()、bsearch()、lsearch(线性搜索)、lfind(线性搜索) * srand(设置随机数种子)、rand(产生随机数) 四、标准预定义宏、断言、typedef * 标准预定义宏的概念 * 断言assert(表达式)相关概念 * 连接...
4. **`lsearch`**:在已排序数组中进行二分查找。 5. **`qsort`**:快速排序。 #### 文件操作篇 1. **`close`**:关闭文件描述符。 2. **`creat`**:创建新文件。 3. **`dup`**:复制文件描述符。 4. **`dup2`**:...
qsort() 函数用于快速排序,bsearch() 用于二分查找,lsearch() 和 lfind() 用于线性搜索。srand() 和 rand() 用于产生随机数,通常需要先调用 srand() 设置随机数种子。 在C语言中,预处理器宏定义和断言也是重要...
* `lsearch`:线性搜索 * ... 这些函数可以用于数据结构和算法的实现,如二分搜索、加密、线性搜索等。 文件操作篇 在C语言中,有多种文件操作函数,例如: * `close`:关闭文件 * `creat`:创建文件 * `dup`:...
8. **数据结构和算法篇**:这里涉及数据结构的处理,如`crypt()`加密数据,`bsearch()`实现二分查找,`lfind()`和`lsearch()`进行线性搜索,`qsort()`实现快速排序,`rand()`和`srand()`用于随机数生成。 9. **文件...
* lsearch(): 线性查找算法 * qsort(): 快速排序算法 * rand(): 生成随机数 * srand(): 设置随机数种子 文件操作篇 * close(): 关闭文件描述符 * creat(): 创建文件 * dup(): 复制文件描述符 * dup2(): 复制文件...
C标准库源代码\LSEARCH.C C标准库源代码\LSEEK.C C标准库源代码\LSEEKI64.C C标准库源代码\MAKEFILE C标准库源代码\MAKEFILE.INC C标准库源代码\MAKEFILE.SUB C标准库源代码\MAKEPATH.C C标准库源代码\MALLOC.C C...