【C++】STL中 sort、priority_queue 自定义 cmp 方法汇总(code c++)


1 sort 函数中的 cmp

1.1 重载小于运算符

sort 重载小于运算符,重载示例如下:

#include <iostream>
#include <algorithm>
using namespace std;

struct Student {
    string name;
    int num;
    int age;
    bool operator<(const Student &stu) const {
        return stu.age < this->age;
    }
};
 
int main() {
    Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
                      {"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
    sort(stu, stu + 5);
    for (int i = 0; i < 5; ++i) {
        cout << stu[i].name << " is " << stu[i].age << endl;
    }
    return 0;
}

1.2 自定义 cmp 函数

sort 自定义 cmp 函数,示例如下:

#include <iostream>
#include <algorithm>
using namespace std;

struct Student {
    string name;
    int num;
    int age;
};

bool cmp(const Student &stu1, const Student &stu2) {
    return stu2.age < stu1.age;
}
 
int main() {
    Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
                      {"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
    sort(stu, stu + 5, cmp);
    for (int i = 0; i < 5; ++i) {
        cout << stu[i].name << " is " << stu[i].age << endl;
    }
    return 0;
}

1.3 自定义仿函数

sort 自定义仿函数,示例如下:

#include <iostream>
#include <algorithm>
using namespace std;

struct Student {
    string name;
    int num;
    int age;
};

struct cmp {
    bool operator()(const Student &stu1, const Student &stu2) {
        return stu2.age < stu1.age;
    }
};

int main() {
    Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
                      {"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
    sort(stu, stu + 5, cmp()); // cmp() 为匿名对象
    for (int i = 0; i < 5; ++i) {
        cout << stu[i].name << " is " << stu[i].age << endl;
    }
    return 0;
}

1.4 自定义 lambda 表达式

sort 自定义 lambda 表达式,示例如下:

#include <iostream>
#include <algorithm>
using namespace std;

struct Student {
    string name;
    int num;
    int age;
};

int main() {
    Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
                      {"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
    sort(stu, stu + 5, 
         [](const Student &stu1, const Student &stu2) { return stu2.age < stu1.age; });
    for (int i = 0; i < 5; ++i) {
        cout << stu[i].name << " is " << stu[i].age << endl;
    }
    return 0;
}

1.5 输出结果(逆序)

年龄从大到小输出,结果如下:

Ma Zi is 21
Wang Er is 20
Li Si is 19
Zhang san is 18
Lucy is 17

2 priority_queue 中的 cmp

2.1 重载小于运算符

priority_queue 重载小于运算符,重载示例如下:

#include <iostream>
#include <queue>
using namespace std;

struct Student {
    string name;
    int num;
    int age;
    bool operator<(const Student &stu) const {
        return stu.age < this->age;
    }
};
 
int main() {
    Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
                      {"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
    priority_queue<Student> q;
    for (int i = 0; i < 5; ++i) {
        q.push(stu[i]);
    }
    while (!q.empty()) {
        cout << q.top().name << " is " << q.top().age << endl;
        q.pop();
    }
    return 0;
}

2.2 自定义仿函数

priority_queue 自定义仿函数,示例如下:

#include <iostream>
#include <queue>
using namespace std;

struct Student {
    string name;
    int num;
    int age;
};

struct cmp {
    bool operator()(const Student &stu1, const Student &stu2) {
        return stu2.age < stu1.age;
    }
};
 
int main() {
    Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
                      {"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
    priority_queue<Student, vector<Student>, cmp> q;
    for (int i = 0; i < 5; ++i) {
        q.push(stu[i]);
    }
    while (!q.empty()) {
        cout << q.top().name << " is " << q.top().age << endl;
        q.pop();
    }
    return 0;
}

2.3 自定义 lambda 表达式

priority_queue 自定义 lambda 表达式,示例如下:

#include <iostream>
#include <queue>
using namespace std;

struct Student {
    string name;
    int num;
    int age;
};

auto cmp = [](const Student &stu1, const Student &stu2) {
    return stu2.age < stu1.age;
};

int main() {
    Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
                      {"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
    priority_queue<Student, vector<Student>, decltype(cmp)> q(cmp);
    for (int i = 0; i < 5; ++i) {
        q.push(stu[i]);
    }
    while (!q.empty()) {
        cout << q.top().name << " is " << q.top().age << endl;
        q.pop();
    }
    return 0;
}

2.4 输出结果(小顶堆)

大顶堆改为小顶堆,年龄从小到大输出,结果如下:

Lucy is 17
Zhang san is 18
Li Si is 19
Wang Er is 20
Ma Zi is 21

3 结论

sort 的 cmp 函数,priority_queue 的 cmp 类型。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

idiot5liev

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

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

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

打赏作者

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

抵扣说明:

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

余额充值