OSDN Git Service

42d7963c8be86bec9659c409b4bb5b5f3d7e3f44
[linuxjm/LDP_man-pages.git] / release / man3 / qsort.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\"
28 .\" Modified 1993-03-29, David Metcalfe
29 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
30 .\" 2006-01-15, mtk, Added example program.
31 .\"
32 .\" FIXME glibc 2.8 added qsort_r(), which needs to be documented.
33 .\"
34 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
35 .\"       all rights reserved.
36 .\" Translated 1997-01-21, YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
37 .\" Updated & Modified 2004-06-06, Yuichi SATO <ysato444@yahoo.co.jp>
38 .\" Updated 2006-01-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
39 .\"
40 .TH QSORT 3 2009-09-15 "" "Linux Programmer's Manual"
41 .SH 名前
42 qsort \- 配列を並べ変える
43 .SH 書式
44 .nf
45 .B #include <stdlib.h>
46 .sp
47 .BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
48 .BI "           int(*" compar ")(const void *, const void *));"
49 .fi
50 .SH 説明
51 .BR qsort ()
52 関数は、
53 \fInmemb\fP 個の大きさ \fIsize\fP の要素をもつ配列を並べ変える。
54 \fIbase\fP 引き数は配列の先頭へのポインタである。
55 .PP
56 \fIcompar\fP をポインタとする比較関数によって、
57 配列の中身は昇順 (値の大きいものほど後に並ぶ順番) に並べられる。
58 比較関数の引き数は比較されるふたつのオブジェクトのポインタである。
59 .PP
60 比較関数は、第一引き数が第二引き数に対して、
61 1) 小さい、2) 等しい、3) 大きいのそれぞれに応じて、
62 1) ゼロより小さい整数、2) ゼロ、3) ゼロより大きい整数の
63 いずれかを返さなければならない。
64 二つの要素の比較結果が等しいとき、
65 並べ変えた後の配列では、これら二つの順序は規定されていない。
66 .SH 返り値
67 .BR qsort ()
68 は値を返さない。
69 .SH 準拠
70 SVr4, 4.3BSD, C89, C99.
71 .SH 注意
72 .I compar
73 引き数に使用するのに適しているライブラリルーチンとしては
74 .BR alphasort (3),
75 .BR versionsort (3)
76 がある。
77 C の文字列を比較する場合、以下の例にあるように比較関数で
78 .BR strcmp (3)
79 を呼び出すこともできる。
80 .SH 例
81 使用例については、
82 .BR bsearch (3)
83 にある例を参照すること。
84
85 以下のプログラムに別の使用例を示す。このプログラムは、
86 コマンドライン引き数で指定された文字列の並び換えを行う。
87 .sp
88 .nf
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92
93 static int
94 cmpstringp(const void *p1, const void *p2)
95 {
96     /* この関数の実際の引き数は "char 型へのポインタのポインタ" だが、
97        strcmp(3) の引き数は "char 型へのポインタ" である。
98        そこで、以下のようにキャストをしてからポインタの逆参照を行う。*/
99
100     return strcmp(* (char * const *) p1, * (char * const *) p2);
101 }
102
103 int
104 main(int argc, char *argv[])
105 {
106     int j;
107
108     if (argc < 2) {
109         fprintf(stderr, "Usage: %s <string>...\\n", argv[0]);
110         exit(EXIT_FAILURE);
111     }
112
113     qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
114
115     for (j = 1; j < argc; j++)
116         puts(argv[j]);
117     exit(EXIT_SUCCESS);
118 }
119 .fi
120 .SH 関連項目
121 .BR sort (1),
122 .BR alphasort (3),
123 .BR strcmp (3),
124 .BR versionsort (3)