qsortのヘルプ・マニュアル
日本語 英語
qsort --help
man qsort
QSORT(3) Linux Programmer’s Manual QSORT(3)
名前
qsort - 配列を並べ変える
書式
#include
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
説明
qsort() 関数は、 nmemb 個の大きさ size の要素をもつ配列を並べ変える。
base 引き数は配列の先頭へのポインタである。
compar をポインタとする比較関数によって、配列の中身は昇順 (値の大きいも
の ほど後に並ぶ順番) に並べられる。比較関数の引き数は比較されるふたつの
オブジェクトのポインタである。
比較関数は、第一引き数が第二引き数に対して、 1) 小さい、2) 等し い 、3)
大 きいのそれぞれに応じて、 1) ゼロより小さい整数、2) ゼロ、3) ゼロより
大きい整数のいずれかを返さなければならない。二つの要素の比較結果が等 し
いとき、並べ変えた後の配列では、これら二つの順序は規定されていない。
返り値
qsort() は値を返さない。
準拠
SVr4, 4.3BSD, C89, C99.
注意
compar 引き数に使用するのに適しているライブラリルーチンとしては alpha-
sort(3), versionsort(3) がある。 C の文字列を比較する場合、以下の例にあ
るように比較関数で strcmp(3) を呼び出すこともできる。
例
使用例については、 bsearch(3) にある例を参照すること。
以 下のプログラムに別の使用例を示す。このプログラムは、コマンドライン引
き数で指定された文字列の並び換えを行う。
#include
#include
#include
#include
static int
cmpstringp(const void *p1, const void *p2)
{
/* この関数の実際の引き数は "char 型へのポインタのポインタ" だが、
strcmp(3) の引き数は "char 型へのポインタ" である。
そこで、以下のようにキャストをしてからポインタの逆参照を行う。*/
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int
main(int argc, char *argv[])
{
int j;
assert(argc > 1);
qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}
関連項目
sort(1), alphasort(3), strcmp(3), versionsort(3)
2009-02-01 QSORT(3)
QSORT(3) Linux Programmer’s Manual QSORT(3)
NAME
qsort - sorts an array
SYNOPSIS
#include
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
DESCRIPTION
The qsort() function sorts an array with nmemb elements of size size.
The base argument points to the start of the array.
The contents of the array are sorted in ascending order according to a
comparison function pointed to by compar, which is called with two
arguments that point to the objects being compared.
The comparison function must return an integer less than, equal to, or
greater than zero if the first argument is considered to be respec-
tively less than, equal to, or greater than the second. If two members
compare as equal, their order in the sorted array is undefined.
RETURN VALUE
The qsort() function returns no value.
CONFORMING TO
SVr4, 4.3BSD, C89, C99.
NOTES
Library routines suitable for use as the compar argument include alpha-
sort(3) and versionsort(3). To compare C strings, the comparison func-
tion can call strcmp(3), as shown in the example below.
EXAMPLE
For one example of use, see the example under bsearch(3).
Another example is the following program, which sorts the strings given
in its command-line arguments:
#include
#include
#include
#include
static int
cmpstringp(const void *p1, const void *p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int
main(int argc, char *argv[])
{
int j;
assert(argc > 1);
qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}
SEE ALSO
sort(1), alphasort(3), strcmp(3), versionsort(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
2009-02-01 QSORT(3)