使用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. 【Win 10 应用开发】将墨迹保存到图像的两种方法

    IT界最近这几年,各种乱七八糟的东西不断出现,其中能用在实际工作与生活中的,大概也就那么几个.Web 前端也冒出各种框架,这就为那些喜欢乱用框架的公司提供了很好的机会,于是造成很多项目体积越来越庞大, ...

  2. Unity3d的模型自动导入帧数表

    开发中经常需要,对美术模型进行一些处理.(以fbx为例) 例如,需要把动作的名字.start和end加入animations的clips. 如果手动操作,就是在模型的Inspector窗口,一个动作点 ...

  3. codeforces 883M. Quadcopter Competition 思路

    M. Quadcopter Competition time limit per test 3 seconds memory limit per test 256 megabytes input st ...

  4. 生成ssl秘钥的方法(纯粹本人记录用的,勿踩)

    openssl genrsa -des3 -out server.key 1024 openssl req -new -key server.key -out server.csr cp server ...

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

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

  6. ABP module-zero +AdminLTE+Bootstrap Table+jQuery权限管理系统第十六节--SignalR与ABP框架Abp.Web.SignalR及扩展

    SignalR简介 SignalR是什么? ASP.NET SignalR 是为 ASP.NET 开发人员提供的一个库,可以简化开发人员将实时 Web 功能添加到应用程序的过程.实时 Web 功能是指 ...

  7. SQL Server 日志清理

    --SQL2005: Backup Log DNName with no_log --这里的DNName是你要收缩的数据库名,自己注意修改下面的数据库名,我就不再注释了. go dump transa ...

  8. oracle用户被锁定

    sqlplus sys/password@localhost:1521/cmsx as sysdba SQL*Plus: Release 11.2.0.1.0 Production on 星期一 7月 ...

  9. C++ Primer高速入门之三:几种常见的控制语句

    语句总是顺序运行的:第一条语句运行完了接着是第二条,第三条等等.这是最简单的情况,为了更好的控制语句的运行.程序设计语言提供了多种控制结构支持更为复杂的语句运行.我们就来看看C++ 提供的控制方式. ...

  10. 结对编程-四则运算GUI的实现

    一.项目成员以及coding地址: 洪灏捷(本人)201321122020  coding地址:https://git.coding.net/hoje/The-GUI-operation.git 白至 ...