STL 的sort函数常用案例

重载小于号的两种方法

方法1:
struct Sum
{
    int s, c, d; // s表示c和d的平方
    bool operator<(const Sum& t) const //返回较小的那个
    {
        if(s != t.s) return s < t.s;
        if(c != t.c) return c < t.c;
        return d < t.d;
    }
} sum[N];

sort(sum, sum + n);
方法2:
struct Sum{
    int s, c, d;
    bool operator()(const Sum & s1, const Sum & s2){
        if(s1.s != s2.s) return s1.s < s2.s;
        if(s1.c != s2.c) return s1.c < s2.c;
        if(s1.d != s2.d) return s1.d < s2.d;
    }
}sum[N];

sort(sum, sum + n, Sum());

案例1

#include <iostream>
#include <algorithm>

using namespace std;

//从大到小排序
struct Rule1{
    bool operator()(const int & a1, const int & a2){
        return a1 > a2;
    }
};

//按个位数从小到大排序
struct Rule2{
    bool operator()(const int & a1, const int & a2){
        return a1 % 10 < a2 % 10;
    }
};

void Print(int *a, int size){
    for(int i = 0; i < size; i ++){
        cout << a[i] << ' ';
    }
    cout << endl;
}

int main(){
    int a[] = {12, 45, 3, 98, 21, 7};
    for(auto e : a) cout << e << ' ';
    cout << endl;

    //从小到大排序
    sort(a, a + sizeof a / sizeof (int));
    cout << "从小到大排序" << endl;
    cout << "1) "; Print(a, sizeof(a) / sizeof(int));

    //从大到小排序
    sort(a, a + sizeof a / sizeof (int),greater<int>());
    cout << "从大到小排序" << endl;
    cout << "2) "; Print(a, sizeof(a) / sizeof(int));

    //从大到小排序
    sort(a, a + sizeof a / sizeof (int), Rule1());
    cout << "从大到小排序" << endl;
    cout << "3) "; Print(a, sizeof(a) / sizeof(int));

    //按余数从小到大排序
    sort(a, a + sizeof a / sizeof (int), Rule2());
    cout << "按个位从小到大排序" << endl;
    cout << "4) "; Print(a, sizeof(a) / sizeof(int));
    return 0;
}

结果输出如下:
12 45 3 98 21 7 
从小到大排序
1) 3 7 12 21 45 98 
从大到小排序
2) 98 45 21 12 7 3 
从大到小排序
3) 98 45 21 12 7 3 
按个位从小到大排序
4) 21 12 3 45 7 98 

案例2

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

//学生信息
struct Student{
    char name[20];
    int id;
    double gpa;
};

//学生信息初始化
Student students [] = {
    {"Jack", 112, 3.4}, {"Mary", 102, 3.8}, {"Mary", 117, 3.9},
    {"Ala", 333, 3.5}, {"Zero", 101, 4.0}
};

//按姓名从小到大排序
struct StudentRule1{
    bool operator()(const Student & s1, const Student & s2){
        if(strcmp(s1.name, s2.name) < 0){
            return true;
        }
        return false;
    }
};

//按id从小到大排序
struct StudentRule2{
    bool operator()(const Student & s1, const Student & s2){
        return s1.id < s2.id;
    }
};

//按gpa从高到低排序
struct StudentRule3{
    bool operator()(const Student & s1, const Student & s2){
        return s1.gpa > s2.gpa;
    }
};

void PrintStudents(Student *s, int size){
    for(int i = 0; i < size; i ++){
        cout << "(" << s[i].name << "," << s[i].id << " " << s[i].gpa << ")" << ' ';
    }
    cout << endl;
}

int main(){
    cout << "学生信息如下:" << endl;
    for(auto e : students){
        cout << e.name << ' ' << e.id << ' ' << e.gpa << ' ';
    }
    cout << endl;
    
    int n = sizeof(students) / sizeof(Student);
    
    cout << "按姓名从小到大排序:" << endl;
    sort(students, students + n, StudentRule1());
    PrintStudents(students, n);
    
    cout << "按id从小到大排序:" << endl;
    sort(students, students + n, StudentRule2());
    PrintStudents(students, n);
    
    cout << "按gpa从高到低排序:" << endl;
    sort(students, students + n, StudentRule3());
    PrintStudents(students, n);
    
    return 0;
}

输出如下:
学生信息如下:
Jack 112 3.4 Mary 102 3.8 Mary 117 3.9 Ala 333 3.5 Zero 101 4 
按姓名从小到大排序:
(Ala,333 3.5) (Jack,112 3.4) (Mary,102 3.8) (Mary,117 3.9) (Zero,101 4) 
按id从小到大排序:
(Zero,101 4) (Mary,102 3.8) (Jack,112 3.4) (Mary,117 3.9) (Ala,333 3.5) 
按gpa从高到低排序:
(Zero,101 4) (Mary,117 3.9) (Mary,102 3.8) (Ala,333 3.5) (Jack,112 3.4) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

soyisou

您的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值