使用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. npm常用命令整理

    npm是一个NodeJS包管理跟分发工具,已经成为了非官方的发布node模块(包)的标准.它可以帮助我们解决代码部署上的一些问题,将开发者从繁琐的包管理工作中(版本.依赖等)解放出来,更加专注于功能上 ...

  2. TensorFlow —— Demo

    import tensorflow as tf g = tf.Graph() # 创建一个Graph对象 在模型中有两个"全局"风格的Variable对象:global_step ...

  3. Echarts后台封装option对象

    该方法返回的keyword指向了前台负责图表显示的jsp页面 public String keyword() { if(this.dateNum == null || this.dateNum.equ ...

  4. THINKPHP增删改查--(改)

    1.CURD 控制器?>namespace Home\Controller;use Think\Controller;class CurdController extends Controlle ...

  5. python中namedtuple介绍

    namedtuple:namedtuple类位于collections模块,有了namedtuple后通过属性访问数据能够让我们的代码更加的直观更好维护.namedtuple能够用来创建类似于元祖的数 ...

  6. PHP-学习之路1

    相信入职快有5个月了,目前项目做过HIS,zySystem,ComStoreSystem当然今天不是来介绍的,后期直到第四个月后APP护身宝经理拍板今后也就是明年正式交于我们团队接手与扩展,运维.虽然 ...

  7. UVA 1426 - Discrete Square Roots(数论)

    UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...

  8. 天津政府应急系统之GIS一张图(arcgis api for flex)解说(三)显示地图坐标系模块

    config.xml文件的配置例如以下: 1 2 <widget left="3" bottom="3" config="widgets/Coo ...

  9. javascript跳跃式前进(3) - 跳入JSON

    前言 JSON崛起不是意外,是顺应时代;相当简洁小巧的书写模式及阅读方式; 基础 看这篇文章: JSON知识点汇总_W3SCHOOL 初步进阶 早期的解析仅仅实用eval() ,可是这货太easy给注 ...

  10. dma_alloc_coherent (建立一致性 DMA 映射函数)

    1.函数申明 /** * dma_alloc_coherent - allocate consistent memory for DMA * @dev: valid struct device poi ...