OSDN Git Service

(split) DP: release pages (catch up to 3.50).
[linuxjm/LDP_man-pages.git] / release / man3 / bsearch.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\" Modified Mon Mar 29 22:41:16 1993, David Metcalfe
30 .\" Modified Sat Jul 24 21:35:16 1993, Rik Faith (faith@cs.unc.edu)
31 .\"*******************************************************************
32 .\"
33 .\" This file was generated with po4a. Translate the source file.
34 .\"
35 .\"*******************************************************************
36 .TH BSEARCH 3 2003\-11\-01 "" "Linux Programmer's Manual"
37 .SH 名前
38 bsearch \- ソートされた配列を二分木検索 (binary search) する
39 .SH 書式
40 .nf
41 \fB#include <stdlib.h>\fP
42 .sp
43 \fBvoid *bsearch(const void *\fP\fIkey\fP\fB, const void *\fP\fIbase\fP\fB,\fP
44 \fB              size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP
45 \fB              int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
46 .fi
47 .SH 説明
48 \fBbsearch\fP()  関数は \fInmemb\fP 個のオブジェクトからなる配列を検索 する。配列の最初のメンバーへのポインタは \fIbase\fP
49 によって与える。 ポインタ \fIkey\fP で参照されるオブジェクトと一致するメンバーが返される。 配列中の各々のメンバーのサイズは \fIsize\fP
50 によって指定する。
51 .PP
52 配列の内容は比較関数 \fIcompar\fP に基づき、昇順にソートされていなけれ ばならない。 \fIcompar\fP ルーチンは二つの引数を取る関数で、一つ
53 目に \fIkey\fP へのポインタ、次に配列のメンバーへのポインタを取る。 この順に指定したとき、 \fIkey\fP が配列メンバーより小さいときには
54 負の整数を、大きいときには正の整数を、一致したときには 0 を、それぞれ \fIcompar\fP は返さなければならない。
55 .SH 返り値
56 \fBbsearch\fP()  関数は、配列のメンバーのうち、一致したものへのポインタを 返す。見つからなかったときは NULL を返す。 \fIkey\fP
57 と一致したメンバーが 複数あるとき、そのうちのどのメンバーが返されるかはわからない。
58 .SH 準拠
59 SVr4, 4.3BSD, POSIX.1\-2001, C89, C99.
60 .SH 例
61 以下の例は、 \fBqsort\fP(3)  を使って構造体の配列の並び換えを行った後、 所望の要素を \fBbsearch\fP()
62 を使って取得するものである。
63 .sp
64 .nf
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68
69 struct mi {
70     int nr;
71     char *name;
72 } months[] = {
73     { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
74     { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
75     { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
76 };
77
78 #define nr_of_months (sizeof(months)/sizeof(months[0]))
79
80 static int
81 compmi(const void *m1, const void *m2)
82 {
83     struct mi *mi1 = (struct mi *) m1;
84     struct mi *mi2 = (struct mi *) m2;
85     return strcmp(mi1\->name, mi2\->name);
86 }
87
88 int
89 main(int argc, char **argv)
90 {
91     int i;
92
93     qsort(months, nr_of_months, sizeof(struct mi), compmi);
94     for (i = 1; i < argc; i++) {
95         struct mi key, *res;
96         key.name = argv[i];
97         res = bsearch(&key, months, nr_of_months,
98                       sizeof(struct mi), compmi);
99         if (res == NULL)
100             printf("\(aq%s\(aq: unknown month\en", argv[i]);
101         else
102             printf("%s: month #%d\en", res\->name, res\->nr);
103     }
104     exit(EXIT_SUCCESS);
105 }
106 .fi
107 .\" this example referred to in qsort.3
108 .SH 関連項目
109 \fBhsearch\fP(3), \fBlsearch\fP(3), \fBqsort\fP(3), \fBtsearch\fP(3)
110 .SH この文書について
111 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.50 の一部
112 である。プロジェクトの説明とバグ報告に関する情報は
113 http://www.kernel.org/doc/man\-pages/ に書かれている。