排序类算法

利用vector进行排序

数字类元素

  每个元素一般包含多个条件,利用lambda编写特定排序条件,用sort完成排序。
涉及的知识点:

  • sort函数以及排序规则
    • 默认是std::less<type>, 即较小的元素在前;std::greater<type>,较大元素先序;
    • 也可以编写自定义排序函数,包括lambda、函数对象、普通函数等。自定义排序规则时,需要明确排序规则,两个元素比较,返回true时,较小元素在前,返回false时,较大元素在前。可以将多个条件进行判断,进行复合排序;
  • lambda函数
struct Info
{
    int age;
    int money;
    int health;
};

void ShowInfo(const Info& info)
{
    cout << info.age << " " << info.health << " " << info.money << endl;;
}

void ShowVec(const vector<Info>& vt)
{
    for (const auto& elem : vt) {
        ShowInfo(elem);
    }
    cout << endl;
}

int main()
{   
    vector<Info> dataVec{
        { 32, 7, 70 },
        { 18, 0, 100 },
        { 23, 8, 90 },
        { 28, 4, 85 },
        { 23, 2, 99 },
        { 37, 8, 65 },
    };

    /* 正能量排序 */
    auto cmp = [](const Info& a, const Info& b){
        if (a.age != b.age) {
            return a.age < b.age;
        } else if (a.health != b.health) {
            return a.health > b.health;
        } else {
            a.money > b.money;
        }

        return true;
    };

    ShowVec(dataVec);

    sort(dataVec.begin(), dataVec.end(), cmp);
    cout << "*************" << endl;
    ShowVec(dataVec);

    return 0;
}

字符串类元素

  字符串排序中多涉及字典序排序。**字典序:**字符串左端对齐,然后逐个字符对比,字符ascii大小相同时,再比较后面的字符;如果前面都相同,但是长度不同,长度小的先序;SLT容器中对string的默认排序都是按照字典序排序的
涉及的知识点:

  • 什么是字典序以及自定义字符串排序规则。STL中容器针对string默认都是按照字典序排序的,包括map、set、priority_queue。
struct Info
{
    string name;
    int age;
    int money;
    int health;
};

void ShowInfo(const Info& info)
{
    cout << info.name << " "<< info.age << " " << info.health << " " << info.money << endl;;
}

void ShowVec(const vector<Info>& vt)
{
    for (const auto& elem : vt) {
        ShowInfo(elem);
    }
    cout << endl;
}

int main()
{   
    vector<Info> dataVec{
        { "abc123", 32, 7, 70 },
        { "bbc123", 18, 0, 100 },
        { "ccc123", 23, 8, 90 },
        { "abc", 28, 4, 85 },
        { "melon", 23, 2, 99 },
        { "jiaojiao", 37, 8, 65 },
    };

    /* 正能量排序 */
    auto cmp1 = [](const Info& a, const Info& b){
        if (a.age != b.age) {
            return a.age < b.age;
        } else if (a.health != b.health) {
            return a.health > b.health;
        } else {
            a.money > b.money;
        }

        return true;
    };

    /* 按照名字字典序排序 */
    auto cmpName = [](const Info& a, const Info& b){
        return a.name < b.name;
    };

    /* 自定义字符串排序规则 */
    auto cmpName2 = [](const Info& a, const Info& b){
        if (a.name.length() != b.name.length()) {
            return a.name.length() < b.name.length();
        } else {
            return a.name < b.name;
        }
    };  

    ShowVec(dataVec);

    sort(dataVec.begin(), dataVec.end(), cmpName2);
    cout << "*************" << endl;
    ShowVec(dataVec);

    return 0;
}

利用其他STL容器排序

map

  其中元素是pair类型,默认先对key排序,key相同后再对value排序。如果可以直接利用map的排序,那就直接用map,但是如果需要重写排序规则,那还是推荐使用vector多维数组,然后编写对应排序规则,这样代码更清晰。如果只是利用map的哈希映射,那推荐使用unorded_map,没有默认排序,也有自动去重功能,效率能高些。

set

  针对单个元素,默认排序,且有自动去重功能。

priority_queue

  找出其中的k个最大元素,greater<>;找出其中k个最小元素,less<>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值