.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" Modified Mon Mar 29 22:41:16 1993, David Metcalfe .\" Modified Sat Jul 24 21:35:16 1993, Rik Faith (faith@cs.unc.edu) .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved. .\" Translated 1998-03-18, NAKANO Takeo .\" Updated 2005-02-26, Akihiro MOTOKI .\" .TH BSEARCH 3 2003\-11\-01 "" "Linux Programmer's Manual" .SH 名前 bsearch \- ソートされた配列を二分木検索 (binary search) する .SH 書式 .nf \fB#include \fP .sp \fBvoid *bsearch(const void *\fP\fIkey\fP\fB, const void *\fP\fIbase\fP\fB,\fP \fB size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP \fB int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP .fi .SH 説明 \fBbsearch\fP() 関数は \fInmemb\fP 個のオブジェクトからなる配列を検索 する。配列の最初のメンバーへのポインタは \fIbase\fP によって与える。 ポインタ \fIkey\fP で参照されるオブジェクトと一致するメンバーが返される。 配列中の各々のメンバーのサイズは \fIsize\fP によって指定する。 .PP 配列の内容は比較関数 \fIcompar\fP に基づき、昇順にソートされていなけれ ばならない。 \fIcompar\fP ルーチンは二つの引き数を取る関数で、一つ 目に \fIkey\fP へのポインタ、次に配列のメンバーへのポインタを取る。 この順に指定したとき、 \fIkey\fP が配列メンバーより小さいときには 負の整数を、大きいときには正の整数を、一致したときには 0 を、それぞれ \fIcompar\fP は返さなければならない。 .SH 返り値 \fBbsearch\fP() 関数は、配列のメンバーのうち、一致したものへのポインタを 返す。見つからなかったときは NULL を返す。 \fIkey\fP と一致したメンバーが 複数あるとき、そのうちのどのメンバーが返されるかはわからない。 .SH 準拠 SVr4, 4.3BSD, POSIX.1\-2001, C89, C99. .SH 例 以下の例は、 \fBqsort\fP(3) を使って構造体の配列の並び換えを行った後、 所望の要素を \fBbsearch\fP() を使って取得するものである。 .sp .nf #include #include #include struct mi { int nr; char *name; } months[] = { { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" }, { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" }, { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" } }; #define nr_of_months (sizeof(months)/sizeof(months[0])) static int compmi(const void *m1, const void *m2) { struct mi *mi1 = (struct mi *) m1; struct mi *mi2 = (struct mi *) m2; return strcmp(mi1\->name, mi2\->name); } int main(int argc, char **argv) { int i; qsort(months, nr_of_months, sizeof(struct mi), compmi); for (i = 1; i < argc; i++) { struct mi key, *res; key.name = argv[i]; res = bsearch(&key, months, nr_of_months, sizeof(struct mi), compmi); if (res == NULL) printf("\(aq%s\(aq: unknown month\en", argv[i]); else printf("%s: month #%d\en", res\->name, res\->nr); } exit(EXIT_SUCCESS); } .fi .\" this example referred to in qsort.3 .SH 関連項目 \fBhsearch\fP(3), \fBlsearch\fP(3), \fBqsort\fP(3), \fBtsearch\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。