zoukankan      html  css  js  c++  java
  • C++中sort的比较函数写法

    原文链接:https://blog.csdn.net/maverick1990/article/details/37738601

    定义排序函数:

    方法1:声明外部比较函数

    bool Less(const Student& s1, const Student& s2)
    {
    return s1.name < s2.name; //从小到大排序
    }
    std::sort(sutVector.begin(), stuVector.end(), Less);
    注意:比较函数必须写在类外部(全局区域)或声明为静态函数


    当comp作为类的成员函数时,默认拥有一个this指针,这样和sort函数所需要使用的排序函数类型不一样。
    否则,会出现<unresolved overloaded function type>错误


    方法2:重载类的比较运算符
    bool operator<(const Student& s1, const Student& s2)
    {
    return s1.name < s2.name; //从小到大排序
    }
    std::sort(sutVector.begin(), stuVector.end());


    方法3:声明比较类
    struct Less
    {
    bool operator()(const Student& s1, const Student& s2)
    {
    return s1.name < s2.name; //从小到大排序
    }
    };

    std::sort(sutVector.begin(), stuVector.end(), Less());


    诸位正值青春年少,一定恣情放纵,贪恋香艳梅施之情,喜欢风流雅韵之事,洒脱木拘。然而诸位可知,草上露一碰即落,竹上霜一触即溶,此种风情难于长久。
  • 相关阅读:
    JUC原子类 1
    线程优先级和守护线程
    多线程中断
    关于html5不支持frameset的解决方法
    shell中$0,$?,$!等的特殊用法
    Linux GCC常用命令
    C/C++中extern关键字详解
    js实现iframe自适应高度
    java线程安全总结
    Linux平台Java调用so库-JNI使用例子
  • 原文地址:https://www.cnblogs.com/shilipojianshen/p/13304614.html
Copyright © 2011-2022 走看看