OSDN Git Service

f27df11c07070040dd9aee7e9e3746a2c03dc6e9
[linuxjm/LDP_man-pages.git] / release / man3 / qsort.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 .\"
30 .\" Modified 1993-03-29, David Metcalfe
31 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
32 .\" 2006-01-15, mtk, Added example program.
33 .\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net>
34 .\"                  and Ben Bacarisse <software@bsb.me.uk>
35 .\"     Document qsort_r()
36 .\"
37 .\"*******************************************************************
38 .\"
39 .\" This file was generated with po4a. Translate the source file.
40 .\"
41 .\"*******************************************************************
42 .TH QSORT 3 2012\-03\-08 "" "Linux Programmer's Manual"
43 .SH 名前
44 qsort, qsort_r \- 配列を並べ変える
45 .SH 書式
46 .nf
47 \fB#include <stdlib.h>\fP
48 .sp
49 \fBvoid qsort(void *\fP\fIbase\fP\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 .sp
52 \fBvoid qsort_r(void *\fP\fIbase\fP\fB, size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP
53 \fB           int (*\fP\fIcompar\fP\fB)(const void *, const void *, void *),\fP
54 \fB           void *\fP\fIarg\fP\fB);\fP
55 .fi
56 .sp
57 .in -4n
58 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
59 .in
60 .sp
61 .ad l
62 \fBqsort_r\fP(): _GNU_SOURCE
63 .ad b
64 .SH 説明
65 \fBqsort\fP()  関数は、 \fInmemb\fP 個の大きさ \fIsize\fP の要素をもつ配列を並べ変える。 \fIbase\fP
66 引き数は配列の先頭へのポインタである。
67 .PP
68 \fIcompar\fP をポインタとする比較関数によって、 配列の中身は昇順 (値の大きいものほど後に並ぶ順番) に並べられる。
69 比較関数の引き数は比較されるふたつのオブジェクトのポインタである。
70 .PP
71 比較関数は、第一引き数が第二引き数に対して、 1) 小さい、2) 等しい、3) 大きいのそれぞれに応じて、 1) ゼロより小さい整数、2) ゼロ、3)
72 ゼロより大きい整数の いずれかを返さなければならない。 二つの要素の比較結果が等しいとき、 並べ変えた後の配列では、これら二つの順序は規定されていない。
73 .PP
74 \fBqsort_r\fP() 関数は \fBqsort\fP() と同じだが、比較関数 \fIcompar\fP が第 3 引き数を
75 取る点が異なる。ポインタが \fIarg\fP 経由で比較関数に渡される。
76 これにより、比較関数は任意の引き数を渡すためにグローバル変数を使う必要がなくなり、
77 そのため、リエントラント (再入可能) で安全にスレッドで使用できるようになる。
78 .SH 返り値
79 関数 \fBqsort\fP() と \fBqsort_r\fP() は値を返さない。
80 .SH バージョン
81 \fBqsort_r\fP() は glibc バージョン 2.8 で追加された。
82 .SH 準拠
83 \fBqsort\fP() 関数は SVr4, 4.3BSD, C89, C99 に準拠している。
84 .SH 注意
85 \fBqsort\fP() の \fIcompar\fP 引き数に使用するのに適しているライブラリルーチンと
86 しては \fBalphasort\fP(3), \fBversionsort\fP(3) がある。 C の文字列を比較する場合、
87 以下の例にあるように比較関数で \fBstrcmp\fP(3) を呼び出すこともできる。
88 .SH 例
89 使用例については、 \fBbsearch\fP(3)  にある例を参照すること。
90
91 以下のプログラムに別の使用例を示す。このプログラムは、 コマンドライン引き数で指定された文字列の並び換えを行う。
92 .sp
93 .nf
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97
98 static int
99 cmpstringp(const void *p1, const void *p2)
100 {
101     /* この関数の実際の引き数は "char 型へのポインタのポインタ" だが、
102        strcmp(3) の引き数は "char 型へのポインタ" である。
103        そこで、以下のようにキャストをしてからポインタの逆参照を行う。*/
104
105     return strcmp(* (char * const *) p1, * (char * const *) p2);
106 }
107
108 int
109 main(int argc, char *argv[])
110 {
111     int j;
112
113     if (argc < 2) {
114         fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
115         exit(EXIT_FAILURE);
116     }
117
118     qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
119
120     for (j = 1; j < argc; j++)
121         puts(argv[j]);
122     exit(EXIT_SUCCESS);
123 }
124 .fi
125 .SH 関連項目
126 \fBsort\fP(1), \fBalphasort\fP(3), \fBstrcmp\fP(3), \fBversionsort\fP(3)
127 .SH この文書について
128 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
129 である。プロジェクトの説明とバグ報告に関する情報は
130 http://www.kernel.org/doc/man\-pages/ に書かれている。