zoukankan      html  css  js  c++  java
  • C++ sort 函数用法

    MSDN中的定义:

    template<class RanIt>
    void sort(RanIt first, RanIt last); //--> 1)
    template<class RanIt, class Pred>
    void sort(RanIt first, RanIt last, Pred pr); //--> 2)


    头文件:
    #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升序排

  • 相关阅读:
    python连接redis
    python3进行md5加密
    python操作mysql数据库
    python3操作excle
    memcache与redis的存储类型
    模块
    函数
    json与字典相互转换
    常用的Random函数
    字符串常用方法
  • 原文地址:https://www.cnblogs.com/ttzm/p/5884588.html
Copyright © 2011-2022 走看看