C++ : Reference : C Library : cstdlib (stdlib.h) : qsort
来源:互联网 发布:手机摄像头测试软件 编辑:程序博客网 时间:2024/04/30 18:34
from: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/
void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
Sort elements of array
Sorts the num elements of the array pointed by base, each element size bytes long, using the comparator function to determine the order.
The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements of the array.
The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.
Output:
The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements of the array.
The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.
Parameters
- base
- Pointer to the first element of the array to be sorted.
- num
- Number of elements in the array pointed by base.
- size
- Size in bytes of each element in the array.
- comparator
- Function that compares two elements. The function shall follow this prototype:
int comparator ( const void * elem1, const void * elem2 );
The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.
The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.
Return Value
(none)Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* qsort example */#include <stdio.h>#include <stdlib.h>int values[] = { 40, 10, 100, 90, 20, 25 };int compare (const void * a, const void * b){ return ( *(int*)a - *(int*)b );}int main (){ int n; qsort (values, 6, sizeof(int), compare); for (n=0; n<6; n++) printf ("%d ",values[n]); return 0;}
Output:
10 20 25 40 90 100
- C++ : Reference : C Library : cstdlib (stdlib.h) : qsort
- C/C++库 stdlib.h cstdlib
- C /C++标准库 - <cstdlib>(stdlib.h)
- cstdlib和stdlib.h
- <cstdlib> (stdlib.h)
- C语言stdlib.h下的qsort<快速排序>
- C语言中你可能不熟悉的头文件<cstdlib>(stdlib.h)
- c-stdlib.h
- 探秘 C stdlib.h
- C/C++笔试必须熟悉掌握的头文件系列(三)——stdlib.h/cstdlib
- C语言stdlib库(#include<stdlib.h>)
- stdlib.h和cstdlib的区别
- <cstdlib> 和 <stdlib.h> 的区别
- C中string.h和stdlib.h
- C语言stdio.h和stdlib.h
- ROS / OpenCV Ubuntu安装“usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: 没有那个文件或目录”解决
- C标准库学习stdlib.h
- C 语言 stdlib.h 库 笔记
- ORACLE对排序的总结
- db2在linux相关指令
- perl发邮件脚本
- 计算机有关的日语单词(九) -IE中的单词
- 计算机有关的日语单词(十) -OutLook中的单词
- C++ : Reference : C Library : cstdlib (stdlib.h) : qsort
- 总是在顶部的层
- java访问Oracle数据库
- 值得收藏的blog
- 计算机有关的日语单词(十) -Office中的单词 (1)
- 电阻、电容、电感及其阻抗、容抗、感抗概念回顾
- URL重写很简单,只要下载一个URLRewriter.dll
- 细数Android平台5大不足
- Samba安装配置