OSDN Git Service

72cefc16055b171f8dcd3a20d8d8243add494b4d
[linuxjm/jm.git] / manual / LDP_man-pages / draft / 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 .\"
43 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
44 .\"       all rights reserved.
45 .\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
46 .\" Updated & Modified 2004-06-06, Yuichi SATO <ysato444@yahoo.co.jp>
47 .\" Updated 2006-01-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
48 .\" Updated 2012-04-30, Akihiro MOTOKI <amotoki@gmail.com>
49 .\"
50 .TH QSORT 3 2020\-11\-01 "" "Linux Programmer's Manual"
51 .SH 名前
52 qsort, qsort_r \- 配列を並べ変える
53 .SH 書式
54 .nf
55 \fB#include <stdlib.h>\fP
56 .PP
57 \fBvoid qsort(void *\fP\fIbase\fP\fB, size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP
58 \fB           int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
59 .PP
60 \fBvoid qsort_r(void *\fP\fIbase\fP\fB, size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP
61 \fB           int (*\fP\fIcompar\fP\fB)(const void *, const void *, void *),\fP
62 \fB           void *\fP\fIarg\fP\fB);\fP
63 .fi
64 .PP
65 .RS -4
66 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
67 .RE
68 .PP
69 .ad l
70 \fBqsort_r\fP(): _GNU_SOURCE
71 .ad b
72 .SH 説明
73 \fBqsort\fP()  関数は、 \fInmemb\fP 個の大きさ \fIsize\fP の要素をもつ配列を並べ変える。 \fIbase\fP
74 引き数は配列の先頭へのポインターである。
75 .PP
76 \fIcompar\fP をポインターとする比較関数によって、 配列の中身は昇順 (値の大きいものほど後に並ぶ順番) に並べられる。
77 比較関数の引き数は比較されるふたつのオブジェクトのポインターである。
78 .PP
79 比較関数は、第一引き数が第二引き数に対して、 1) 小さい、2) 等しい、3) 大きいのそれぞれに応じて、 1) ゼロより小さい整数、2) ゼロ、3)
80 ゼロより大きい整数の いずれかを返さなければならない。 二つの要素の比較結果が等しいとき、 並べ変えた後の配列では、これら二つの順序は規定されていない。
81 .PP
82 \fBqsort_r\fP() 関数は \fBqsort\fP() と同じだが、比較関数 \fIcompar\fP が第 3 引き数を
83 取る点が異なる。ポインターが \fIarg\fP 経由で比較関数に渡される。
84 これにより、比較関数は任意の引き数を渡すためにグローバル変数を使う必要がなくなり、
85 そのため、リエントラント (再入可能) で安全にスレッドで使用できるようになる。
86 .SH 返り値
87 関数 \fBqsort\fP() と \fBqsort_r\fP() は値を返さない。
88 .SH バージョン
89 \fBqsort_r\fP() は glibc バージョン 2.8 で追加された。
90 .SH 属性
91 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。
92 .TS
93 allbox;
94 lbw18 lb lb
95 l l l.
96 インターフェース        属性  値
97 T{
98 \fBqsort\fP(),
99 \fBqsort_r\fP()
100 T}      Thread safety   MT\-Safe
101 .TE
102 .sp 1
103 .SH 準拠
104 \fBqsort\fP(): POSIX.1\-2001, POSIX.1\-2008, C89, C99, SVr4, 4.3BSD.
105 .SH 注意
106 To compare C strings, the comparison function can call \fBstrcmp\fP(3), as
107 shown in the example below.
108 .SH EXAMPLES
109 使用例については、 \fBbsearch\fP(3)  にある例を参照すること。
110 .PP
111 以下のプログラムに別の使用例を示す。このプログラムは、 コマンドライン引き数で指定された文字列の並び換えを行う。
112 .PP
113 .EX
114 #include <stdio.h>
115 #include <stdlib.h>
116 #include <string.h>
117
118 static int
119 cmpstringp(const void *p1, const void *p2)
120 {
121     /* この関数の実際の引き数は "char 型へのポインターのポインター" だが、
122        strcmp(3) の引き数は "char 型へのポインター" である。
123        そこで、以下のようにキャストをしてからポインターの逆参照を行う。*/
124
125     return strcmp(*(const char **) p1, *(const char **) p2);
126 }
127
128 int
129 main(int argc, char *argv[])
130 {
131     if (argc < 2) {
132         fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
133         exit(EXIT_FAILURE);
134     }
135
136     qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
137
138     for (int j = 1; j < argc; j++)
139         puts(argv[j]);
140     exit(EXIT_SUCCESS);
141 }
142 .EE
143 .SH 関連項目
144 \fBsort\fP(1), \fBalphasort\fP(3), \fBstrcmp\fP(3), \fBversionsort\fP(3)
145 .SH この文書について
146 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
147 \%https://www.kernel.org/doc/man\-pages/ に書かれている。