|
��������
bsearch - �������� ����� � ��������������� �������
���������
#include <search.h> char *bsearch ((char *) key, (char *) base, nel, sizeof (*key), compar) unsigned nel; int (*compar) ( );
��������
������� bsearch ������������� ��� ���������� ���������
������ � ������������ � ����������, ��������� � �����
�. �����: ��������� ���������������� ��� ���. �. 3.
����������, �����. - �.: ���, 1978. ������ 6.2.1, �������� B.
������� bsearch ���������� ��������� ������ ������� �� ������� ������. �������������� ������� ������ ���� ������������� � ������������ ������� �������� ��������������� ������� ���������. �������� key ��������� �� �� ��� ������, ������������� � ������� (���� ������); base ��������� �� ������ ������� �������; nel ������ ���������� ��������� � �������; compar - ��� ������� ���������, ����������� ������� ��� ������ ������ ��� ��������� �� ������������ ��������. � ������������ � ���, ����� ����� ����� ��� ����������: ������� ����, ������ ���� ��� ������� ����, ������ �������� ��������� �������, ������ ��� ������� �� ��������� �� �������.
������
��������� ������ ������������� ����� � �������, ���������� ��������� �� ����, ������� ������� �� �������
�������� � �� ����. ������� ������������� � ����������
������� �� �������� �������� � �����.
�������� �������� ���������, ������� ��������� ������� �������� � ���� ��������������� ����, ����� ������������� ������� �������� � �� ������ ��� ������� ��������� �� ������.
#include <stdio.h> #include <search.h> #define TABSIZE 1000 struct node { /* ��������� ����� */ char *string; int length; }; struct node table [TABSIZE]; /* ������� ��� ������ */ ... { struct node *node_ptr, node; int node_compare (); /* ������� ��������� ����� */ char str_space [20]; /* ������������ ��� ����� ������� �������� */ ... node.string = str_space; while (scanf ("%s", node.string) != EOF) { node_ptr = (struct node *) bsearch ((char *) (&node), (char *) table, TABSIZE, sizeof (struct node), node_compare); if (node_ptr != NULL) { (void) printf ("string = %20s, length = %d\n", node_ptr->string, node_ptr->length); } else { (void) printf ("not found:%s\n", node.string); } } } /* ��������� ������� ���������� ��� ���� �� �������� �������� �� ������ ����������� ������� */ int node_compare (node1, node2) char *node1, *node2; { return strcmp (((struct node *) node1) -> string, ((struct node *) node2) -> string); }
����������
��������� �� ���� (key) � �� ������ ������� �������
(base) ������ ����� ��� "��������� �� �������" � ����������������� � ���� "��������� �� ������".
� ���������, �������������� �������� compar, �� ����������� ������ ����������� ������ ����, ������� �������� ������� � ���������� � ������������ ��������� ����� ��������� ������������ ������.
���� ������� bsearch ����������� ��� ������� ��� "��������� �� ������", ������������ �� �������� ������� ��������������� � ���� "��������� �� �������".
��. �����
hsearch(3C), lsearch(3C), qsort(3C), tsearch(3C).
�����������
� ������ ���������� ������ ����������� ������ ������
��������� NULL
|