.\" 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 1993-03-29, David Metcalfe .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu) .\" 2006-01-15, mtk, Added example program. .\" Modified 2012-03-08, Mark R. Bannister .\" and Ben Bacarisse .\" Document qsort_r() .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi .\" all rights reserved. .\" Translated 1997-01-21, YOSHINO Takashi .\" Updated & Modified 2004-06-06, Yuichi SATO .\" Updated 2006-01-18, Akihiro MOTOKI .\" Updated 2012-04-30, Akihiro MOTOKI .\" .TH QSORT 3 2020\-11\-01 "" "Linux Programmer's Manual" .SH 名前 qsort, qsort_r \- 配列を並べ変える .SH 書式 .nf \fB#include \fP .PP \fBvoid qsort(void *\fP\fIbase\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 .PP \fBvoid qsort_r(void *\fP\fIbase\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 *, void *),\fP \fB void *\fP\fIarg\fP\fB);\fP .fi .PP .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .RE .PP .ad l \fBqsort_r\fP(): _GNU_SOURCE .ad b .SH 説明 \fBqsort\fP() 関数は、 \fInmemb\fP 個の大きさ \fIsize\fP の要素をもつ配列を並べ変える。 \fIbase\fP 引数は配列の先頭へのポインターである。 .PP \fIcompar\fP をポインターとする比較関数によって、 配列の中身は昇順 (値の大きいものほど後に並ぶ順番) に並べられる。 比較関数の引数は比較されるふたつのオブジェクトのポインターである。 .PP 比較関数は、第一引数が第二引数に対して、 1) 小さい、2) 等しい、3) 大きいのそれぞれに応じて、 1) ゼロより小さい整数、2) ゼロ、3) ゼロより大きい整数の いずれかを返さなければならない。 二つの要素の比較結果が等しいとき、 並べ変えた後の配列では、これら二つの順序は規定されていない。 .PP \fBqsort_r\fP() 関数は \fBqsort\fP() と同じだが、比較関数 \fIcompar\fP が第 3 引数を 取る点が異なる。ポインターが \fIarg\fP 経由で比較関数に渡される。 これにより、比較関数は任意の引数を渡すためにグローバル変数を使う必要がなくなり、 そのため、リエントラント (再入可能) で安全にスレッドで使用できるようになる。 .SH 返り値 関数 \fBqsort\fP() と \fBqsort_r\fP() は値を返さない。 .SH バージョン \fBqsort_r\fP() は glibc バージョン 2.8 で追加された。 .SH 属性 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。 .TS allbox; lbw18 lb lb l l l. インターフェース 属性 値 T{ \fBqsort\fP(), \fBqsort_r\fP() T} Thread safety MT\-Safe .TE .sp 1 .SH 準拠 \fBqsort\fP(): POSIX.1\-2001, POSIX.1\-2008, C89, C99, SVr4, 4.3BSD. .SH 注意 C の文字列を比較する場合、以下の例にあるように比較関数で \fBstrcmp\fP(3) を呼び出すこともできる。 .SH 例 使用例については、 \fBbsearch\fP(3) にある例を参照すること。 .PP 以下のプログラムに別の使用例を示す。このプログラムは、 コマンドライン引数で指定された文字列の並び換えを行う。 .PP .EX #include #include #include static int cmpstringp(const void *p1, const void *p2) { /* この関数の実際の引数は "char 型へのポインターのポインター" だが、 strcmp(3) の引数は "char 型へのポインター" である。 そこで、以下のようにキャストをしてからポインターの逆参照を行う。*/ return strcmp(*(const char **) p1, *(const char **) p2); } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s ...\en", argv[0]); exit(EXIT_FAILURE); } qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); for (int j = 1; j < argc; j++) puts(argv[j]); exit(EXIT_SUCCESS); } .EE .SH 関連項目 \fBsort\fP(1), \fBalphasort\fP(3), \fBstrcmp\fP(3), \fBversionsort\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。