zoukankan      html  css  js  c++  java
  • C/c++中关于qsort函数的

    参考原文:https://www.cnblogs.com/CCBB/archive/2010/01/15/1648827.html

    http://www.cplusplus.com/reference/cstdlib/qsort/

    1.函数原型:

    void qsort (void* base, size_t num, size_t size,
                int (*compar)(const void*,const void*));
    
    /*base: 
    *Pointer to the first object of the array to be sorted, converted to a *void*. */
    
    /*num:
    *Number of elements in the array pointed to by base.
    *size_t is an unsigned integral type.
    */
    /*size:
    *Size in bytes of each element in the array.
    *size_t is an unsigned integral type.
    */
    /*compar:    
    int compar (const void* p1, const void* p2);
    *如果返回值<0, p1指向的元素排在p2指向的元素之前
    *如果==0,两元素
    *返回值>0,p1指向的元素排在p2指向的元素之前
    */

    2.使用例子

    /* qsort example */
    #include <stdio.h>      /* printf */
    #include <stdlib.h>     /* qsort */
    
    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;
    }

    输出为 :

    10 20 25 40 90 100
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    杂项
    导出查询数据(大数据量)
    设置现有字段自增
    C++ 矩形交集和并集的面积-离散化
    Python使用flask架构、跨域
    匈牙利命名法
    C++ main函数
    windows编译boost
    mfc HackerTools监控键盘按键
    mfc HackerTools远程线程注入
  • 原文地址:https://www.cnblogs.com/Shinered/p/9786522.html
Copyright © 2011-2022 走看看