OSDN Git Service

575a53dc652867f246d2658cc91f81a3f1ce1609
[linuxjm/LDP_man-pages.git] / draft / man3 / bsearch.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\" Modified Mon Mar 29 22:41:16 1993, David Metcalfe
28 .\" Modified Sat Jul 24 21:35:16 1993, Rik Faith (faith@cs.unc.edu)
29 .\"
30 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
31 .\" Translated 1998-03-18, NAKANO Takeo <nakano@apm.seikei.ac.jp>
32 .\" Updated 2005-02-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
33 .\"
34 .TH BSEARCH 3  2003-11-01 "" "Linux Programmer's Manual"
35 .\"O .SH NAME
36 .SH 名前
37 .\"O bsearch \- binary search of a sorted array
38 bsearch \- ソートされた配列を二分木検索 (binary search) する
39 .\"O .SH SYNOPSIS
40 .SH 書式
41 .nf
42 .B #include <stdlib.h>
43 .sp
44 .BI "void *bsearch(const void *" key ", const void *" base ,
45 .BI "              size_t " nmemb ", size_t " size ,
46 .BI "              int (*" compar ")(const void *, const void *));"
47 .fi
48 .\"O .SH DESCRIPTION
49 .SH 説明
50 .\"O The
51 .\"O .BR bsearch ()
52 .\"O function searches an array of \fInmemb\fP objects,
53 .\"O the initial member of which is pointed to by \fIbase\fP, for a member
54 .\"O that matches the object pointed to by \fIkey\fP.
55 .\"O The size of each member
56 .\"O of the array is specified by \fIsize\fP.
57 .BR bsearch ()
58 関数は \fInmemb\fP 個のオブジェクトからなる配列を検索
59 する。配列の最初のメンバーへのポインタは \fIbase\fP によって与える。
60 ポインタ \fIkey\fP で参照されるオブジェクトと一致するメンバーが返される。
61 配列中の各々のメンバーのサイズは \fIsize\fP によって指定する。
62 .PP
63 .\"O The contents of the array should be in ascending sorted order according
64 .\"O to the comparison function referenced by \fIcompar\fP.
65 .\"O The \fIcompar\fP
66 .\"O routine is expected to have two arguments which point to the \fIkey\fP
67 .\"O object and to an array member, in that order, and should return an integer
68 .\"O less than, equal to, or greater than zero if the \fIkey\fP object is found,
69 .\"O respectively, to be less than, to match, or be greater than the array
70 .\"O member.
71 配列の内容は比較関数 \fIcompar\fP に基づき、昇順にソートされていなけれ
72 ばならない。 \fIcompar\fP ルーチンは二つの引数を取る関数で、一つ
73 目に \fIkey\fP へのポインタ、次に配列のメンバーへのポインタを取る。
74 この順に指定したとき、 \fIkey\fP が配列メンバーより小さいときには
75 負の整数を、大きいときには正の整数を、一致したときには 0 を、それぞれ
76 \fIcompar\fP は返さなければならない。
77 .\"O .SH "RETURN VALUE"
78 .SH 返り値
79 .\"O The
80 .\"O .BR bsearch ()
81 .\"O function returns a pointer to a matching member of the
82 .\"O array, or NULL if no match is found.  If there are multiple elements that
83 .\"O match the key, the element returned is unspecified.
84 .BR bsearch ()
85 関数は、配列のメンバーのうち、一致したものへのポインタを
86 返す。見つからなかったときは NULL を返す。 \fIkey\fP と一致したメンバーが
87 複数あるとき、そのうちのどのメンバーが返されるかはわからない。
88 .\"O .SH "CONFORMING TO"
89 .SH 準拠
90 SVr4, 4.3BSD, POSIX.1-2001, C89, C99.
91 .\"O .SH EXAMPLE
92 .SH 例
93 .\"O The example below first sorts an array of structures using
94 .\"O .BR qsort (3),
95 .\"O then retrieves desired elements using
96 .\"O .BR bsearch ().
97 以下の例は、
98 .BR qsort (3)
99 を使って構造体の配列の並び換えを行った後、
100 所望の要素を
101 .BR bsearch ()
102 を使って取得するものである。
103 .sp
104 .nf
105 #include <stdio.h>
106 #include <stdlib.h>
107 #include <string.h>
108
109 struct mi {
110     int nr;
111     char *name;
112 } months[] = {
113     { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
114     { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
115     { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
116 };
117
118 #define nr_of_months (sizeof(months)/sizeof(months[0]))
119
120 static int
121 compmi(const void *m1, const void *m2)
122 {
123     struct mi *mi1 = (struct mi *) m1;
124     struct mi *mi2 = (struct mi *) m2;
125     return strcmp(mi1\->name, mi2\->name);
126 }
127
128 int
129 main(int argc, char **argv)
130 {
131     int i;
132
133     qsort(months, nr_of_months, sizeof(struct mi), compmi);
134     for (i = 1; i < argc; i++) {
135         struct mi key, *res;
136         key.name = argv[i];
137         res = bsearch(&key, months, nr_of_months,
138                       sizeof(struct mi), compmi);
139         if (res == NULL)
140             printf("\(aq%s\(aq: unknown month\en", argv[i]);
141         else
142             printf("%s: month #%d\en", res\->name, res\->nr);
143     }
144     exit(EXIT_SUCCESS);
145 }
146 .fi
147 .\"O .\" this example referred to in qsort.3
148 .\" この例は qsort.3 で参照されている。
149 .\"O .SH "SEE ALSO"
150 .SH 関連項目
151 .BR hsearch (3),
152 .BR lsearch (3),
153 .BR qsort (3),
154 .BR tsearch (3)