OSDN Git Service

5bc79c7aee187e18b34fbaaae04524ee0eef0ddd
[linuxjm/jm.git] / manual / LDP_man-pages / draft / 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 .\"
37 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
38 .\" Translated 1998-03-18, NAKANO Takeo <nakano@apm.seikei.ac.jp>
39 .\" Updated 2005-02-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
40 .\"
41 .TH BSEARCH 3 2020\-11\-01 "" "Linux Programmer's Manual"
42 .SH 名前
43 bsearch \- ソートされた配列を二分木検索 (binary search) する
44 .SH 書式
45 .nf
46 \fB#include <stdlib.h>\fP
47 .PP
48 \fBvoid *bsearch(const void *\fP\fIkey\fP\fB, const void *\fP\fIbase\fP\fB,\fP
49 \fB              size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP
50 \fB              int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
51 .fi
52 .SH 説明
53 \fBbsearch\fP()  関数は \fInmemb\fP 個のオブジェクトからなる配列を検索 する。配列の最初のメンバーへのポインターは \fIbase\fP
54 によって与える。 ポインター \fIkey\fP で参照されるオブジェクトと一致するメンバーが返される。 配列中の各々のメンバーのサイズは \fIsize\fP
55 によって指定する。
56 .PP
57 配列の内容は比較関数 \fIcompar\fP に基づき、昇順にソートされていなけれ ばならない。 \fIcompar\fP
58 ルーチンは二つの引き数を取る関数で、一つ 目に \fIkey\fP へのポインター、次に配列のメンバーへのポインターを取る。 この順に指定したとき、
59 \fIkey\fP が配列メンバーより小さいときには 負の整数を、大きいときには正の整数を、一致したときには 0 を、それぞれ \fIcompar\fP
60 は返さなければならない。
61 .SH 返り値
62 \fBbsearch\fP()  関数は、配列のメンバーのうち、一致したものへのポインターを 返す。見つからなかったときは NULL を返す。 \fIkey\fP
63 と一致したメンバーが 複数あるとき、そのうちのどのメンバーが返されるかはわからない。
64 .SH 属性
65 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。
66 .TS
67 allbox;
68 lb lb lb
69 l l l.
70 インターフェース        属性  値
71 T{
72 \fBbsearch\fP()
73 T}      Thread safety   MT\-Safe
74 .TE
75 .sp 1
76 .SH 準拠
77 POSIX.1\-2001, POSIX.1\-2008, C89, C99, SVr4, 4.3BSD.
78 .SH EXAMPLES
79 以下の例は、 \fBqsort\fP(3)  を使って構造体の配列の並び換えを行った後、 所望の要素を \fBbsearch\fP()
80 を使って取得するものである。
81 .PP
82 .EX
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86
87 struct mi {
88     int nr;
89     char *name;
90 } months[] = {
91     { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
92     { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
93     { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
94 };
95
96 #define nr_of_months (sizeof(months)/sizeof(months[0]))
97
98 static int
99 compmi(const void *m1, const void *m2)
100 {
101     const struct mi *mi1 = m1;
102     const struct mi *mi2 = m2;
103     return strcmp(mi1\->name, mi2\->name);
104 }
105
106 int
107 main(int argc, char **argv)
108 {
109     qsort(months, nr_of_months, sizeof(months[0]), compmi);
110     for (int i = 1; i < argc; i++) {
111         struct mi key;
112         struct mi *res;
113
114         key.name = argv[i];
115         res = bsearch(&key, months, nr_of_months,
116                       sizeof(months[0]), compmi);
117         if (res == NULL)
118             printf("\(aq%s\(aq: unknown month\en", argv[i]);
119         else
120             printf("%s: month #%d\en", res\->name, res\->nr);
121     }
122     exit(EXIT_SUCCESS);
123 }
124 .EE
125 .\" this example referred to in qsort.3
126 .SH 関連項目
127 \fBhsearch\fP(3), \fBlsearch\fP(3), \fBqsort\fP(3), \fBtsearch\fP(3)
128 .SH この文書について
129 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
130 \%https://www.kernel.org/doc/man\-pages/ に書かれている。