使用qsort 需要包含头文件#include<algorithm>

例子:

class Wooden
{
public:
int weight;
int length;
bool flag;
public:
Wooden(int lenght=0,int weight=0)
{
this->weight=weight;
this->length=lenght;
flag=true;
}
};

比较函数:
int Compare_wooden(const void* t1,const void* t2)
{
Wooden* w1;
Wooden* w2;
w1=(Wooden*)t1;
w2=(Wooden*)t2;
return w1->length-w2->length;
}

qsort(arrays,countswooden,sizeof(Wooden),Compare_wooden);

1)why你必须给予元素个数?

因为阵列不知道它自己有多少个元素

2)why你必须给予大小?

因为 qsort 不知道它要排序的单位.

3)why你必须写那个丑陋的、用来比较俩数值的函式?

因为 qsort 需要一个指标指向某个函式,因为它不知道它所要排序的元素型别.

4)why qsort 所使用的比较函式接受的是 const void* 引数而不是 char* 引数?

因为 qsort 可以对非字串的数值排序.

原文链接:http://blog.csdn.net/zzzmmmkkk/article/details/4266888/

C++ qsort的更多相关文章

  1. 排序算法----调用库函数qsort进行快速排序

    功 能: 快速排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const ...

  2. QSort函数对不同类型数据快速排序浅谈

    一.对int类型数组排序 int num[100]; int cmp ( const void *a , const void *b ){return *(int *)a - *(int *)b;} ...

  3. 快排 快速排序 qsort quicksort C语言

    现在网上搜到的快排和我以前打的不太一样,感觉有点复杂,我用的快排是FreePascal里/demo/text/qsort.pp的风格,感觉特别简洁. #include<stdio.h> # ...

  4. 如何使用C自带的qsort快速排序

    / you can write to stdout for debugging purposes, e.g. // printf("this is a debug message\n&quo ...

  5. c/c++ qsort 函数 结构体简单使用(1)

    #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct student ...

  6. c/c++ qsort 函数的简单使用(1)

    #include <stdio.h> #include <stdlib.h> //打印数组元素 void print(int arr[], int n){ ; i < n ...

  7. qsort C++ VS2013 leetcode

    class Solution { private: static int compare(const void * a, const void * b) { return (*(int*)a - *( ...

  8. qsort函数、sort函数【转】

    http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...

  9. C中的qsort函数和C++中的sort函数的理解与使用

    一.qsort()函数 原型:_CRTIMP void __cdecl qsort (void*, size_t, size_t,int (*)(const void*, const void*)); ...

  10. qsort库函数的用法

    qsort 功 能: 使用快速排序例程进行排序  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *, ...

随机推荐

  1. liunx

    一键安装地址:https://lnmp.org/install.html

  2. Centos下安装git的web服务器

    直接上代码 [Shell] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  3. 分享一小坑(与swagger有关),以后碰到了可以快速规避

     ---------------------------------------------------------------------------------踩坑过程:①webapi的某acti ...

  4. 【NOIP2014提高组】联合权值

    https://www.luogu.org/problem/show?pid=1351 既然是一棵树,就先转化成有根树.有根树上距离为2的点对,路径可能长下面这样: 枚举路径上的中间点X. 第一种情况 ...

  5. NOIP2016提高组初赛(2)四、读程序写结果3、求最长回文子序列

    #include <iostream> using namespace std; int lps(string seq, int i, int j) { int len1, len2; i ...

  6. kafak集群安装-转

    前言 最近在利用Spark streaming和Kafka构建一个实时的数据分析系统,对图书阅读数据进行分析,做实时推荐.Spark Streaming 模块是对于 Spark Core 的一个扩展, ...

  7. 用HTML5实现的各种排序算法的动画比較

    用HTML5实现的各种排序算法的动画比較 非常有意思,详见: http://www.webhek.com/misc/comparison-sort/

  8. Android setContentView方法解析(一)

    在Activity的生命周期onCreate中.我们一般都习惯性的调用setContentView(int layoutResID)方法,把布局文件载入到页面上来.以下我们就来通过源代码一步步的分析怎 ...

  9. ElasticSearch和ElasticSearch Head环境搭建和数据模拟

    首先elasticsearch-6.0.0\bin目录下运行elasticsearch服务 修改elasticsearch-6.0.0\elasticsearch.yml文件 在文件最后加入下面代码, ...

  10. 七、Spring Boot Servlet 使用

    Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet.Filter.Listener.Interceptor 等等. 当使用spring-Boot时,嵌 ...