• sort使用


    #include <algorithm>
    using namespace std;


    1.默认的sort函数是按升序排。对应于1)
    sort(a,a+n);   //两个参数分别为待排序数组的首地址和尾地址
    2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)
    例如:
    int cmp( const int &a, const int &b ){
        if( a > b )
           return 1;
        else
           return 0;
    }
    sort(a,a+n,cmp);
    是对数组a降序排序
    又如:
    int cmp( const POINT &a, const POINT &b ){
        if( a.x < b.x )
           return 1;
        else
           if( a.x == b.x ){
              if( a.y < b.y )
                 return 1;
              else
                 return 0;
            }
           else
              return 0;
    }
    sort(a,a+n,cmp);
    是先按x升序排序,若x值相等则按y升序排


    与此类似的还有C中的qsort,以下同附上qsort的使用方法:
    #include <stdlib.h>
             格式 qsort(array_name,data_number,sizeof(data_type),compare_function_name)       (void*)bsearch (pointer_to_key_word,array_name,find_number,
    sizeof(data_type),compare_function_name)
             e.g.
    int Cmp(const void*a,const void *b)
    {
                      int*pa=(int*)a,*pb=(int*)b;
                      if(*pa>*pb) return 1;
                 else if (*pa==*pb)    return 0;
    else   return -1;
    }


    qsort(data,N,sizeof(int),Cmp);        // 对int型数组进行快速排序(非降序排列)
    p=(int*)bsearch(&a,data,n,sizeof(int),Cmp);

  • 相关阅读:
    获取并设置ListView高度的方法
    Android的webview加载本地html、本apk内html和远程URL
    Android
    jQuery通知插件noty
    腾讯地图之Marker
    星星打分
    eval以及json
    PHP json_encode
    javascript 检测密码强度 美化版
    javascript 检测密码强度
  • 原文地址:https://www.cnblogs.com/zlyblog/p/3043933.html
Copyright © 2020-2023  润新知