c++ sort 自定义排序

一、c++ sort 自定义排序

在 C++ 中,使用 std::sort 可以通过传递自定义的比较函数来实现自定义排序。自定义排序需要定义一个比较函数或函数对象(可以是一个函数指针、lambda 表达式,或者一个实现了 operator() 的类),并将它作为第三个参数传递给 std::sort

示例 1:使用自定义比较函数

#include <iostream>
#include <vector>
#include <algorithm>

// 自定义比较函数
bool customCompare(int a, int b) {
    return a > b; // 降序排序
}

int main() {
    std::vector<int> vec = {5, 2, 9, 1, 5, 6};

    // 使用自定义比较函数进行排序
    std::sort(vec.begin(), vec.end(), customCompare);

    // 输出排序后的结果
    for (int v : vec) {
        std::cout << v << " ";
    }
    return 0;
}

示例 2:使用 Lambda 表达式

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {5, 2, 9, 1, 5, 6};

    // 使用 Lambda 表达式进行排序,按降序排序
    std::sort(vec.begin(), vec.end(), [](int a, int b) {
        return a > b;
    });

    // 输出排序后的结果
    for (int v : vec) {
        std::cout << v << " ";
    }
    return 0;
}

示例 3:使用函数对象(仿函数)

#include <iostream>
#include <vector>
#include <algorithm>

// 定义函数对象(仿函数)
struct CustomCompare {
    bool operator()(int a, int b) const {
        return a > b; // 降序排序
    }
};

int main() {
    std::vector<int> vec = {5, 2, 9, 1, 5, 6};

    // 使用自定义的函数对象进行排序
    std::sort(vec.begin(), vec.end(), CustomCompare());

    // 输出排序后的结果
    for (int v : vec) {
        std::cout << v << " ";
    }
    return 0;
}

示例 4:对象排序

#include <iostream>
#include <vector>
#include <algorithm>

// 定义一个对象类型
struct Person {
    std::string name;
    int age;

    Person(std::string n, int a) : name(n), age(a) {}
};

// 自定义比较函数,按年龄升序排序
bool comparePerson(const Person &p1, const Person &p2) {
    return p1.age < p2.age;
}

int main() {
    std::vector<Person> people = { Person("Alice", 30), Person("Bob", 20), Person("Charlie", 25) };

    // 按年龄排序
    std::sort(people.begin(), people.end(), comparePerson);

    // 输出排序后的结果
    for (const auto &p : people) {
        std::cout << p.name << ": " << p.age << std::endl;
    }

    return 0;
}

解释

  1. 自定义比较函数bool customCompare(int a, int b) 定义了排序的逻辑,例如降序排序时返回 a > b
  2. Lambda 表达式:简洁地定义了一个比较函数,常用于临时自定义排序。
  3. 仿函数:通过类的 operator() 实现自定义逻辑,提供了更灵活的结构化代码。
  4. 对象排序:通过自定义比较函数对对象排序,根据某个属性(如年龄)排序。

这几种方式都可以让你根据自己的需求对容器中的数据进行自定义排序。

二、a > b 为什么是降序

在 C++ 中,自定义排序函数用于决定两个元素的相对顺序。std::sort 函数的比较函数通常是二元谓词,它应该返回一个布尔值来指示元素的顺序。

比较函数的规则是这样的:

  • 如果返回 true,那么第一个参数会被排在第二个参数之前。
  • 如果返回 false,则第一个参数会被排在第二个参数之后。

a > b 是降序的原因:

当我们写 a > b 作为比较函数时,意味着:

  • ab 大时,返回 true,即 a 应该排在 b 的前面。
  • 因为较大的值会被放在较小值的前面,这就形成了降序排列。

具体例子:

假设我们有一组数字 {1, 3, 2}

  • 当使用 a > b 作为比较函数时:
    1. 比较 131 > 3false,因此 3 应该排在 1 前面。
    2. 比较 323 > 2true,因此 3 继续保持在前面。
    3. 最终的顺序是:3, 2, 1

对比:

如果我们使用 a < b,则会形成升序排序:

  • a 小于 b 时返回 true,所以较小的值会被放在前面,形成升序排列。

因此,a > b 实现了降序排序,而 a < b 实现了升序排序。

示例:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 3, 2};

    // 使用 a > b 实现降序排序
    std::sort(vec.begin(), vec.end(), [](int a, int b) {
        return a > b; // 降序
    });

    // 输出排序结果
    for (int num : vec) {
        std::cout << num << " ";  // 输出:3 2 1
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值