STL sort详解

参考博客:https://blog.csdn.net/wangshubo1989/article/details/52806027

最近用到c++ STL中的sort函数,由于不熟悉,查找的博客中也有些小漏洞,走了些弯路。所以自己摘录修改了一篇博客以便记录。

1 数组排序

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

int main()
{
    int test_arr[5];
    test_arr[0] = 2;
    test_arr[1] = 3;
    test_arr[2] = 5;
    atest_arrr[3] = 1;
    test_arr[4] = 4;

    //默认升序排列
    sort(test_arr,test_arr+5);

    cout << "升序排列: " << endl;
    cout << test_arr[0] << ",";
    cout << test_arr[1] << ",";
    cout << test_arr[2] << ",";
    cout << test_arr[3] << ",";
    cout << test_arr[4] << endl;

    //降序排列
    sort(test_arr,test_arr+5,greater<int>());

    cout << "降序排列: " << endl;
    cout << test_arr[0] << ",";
    cout << test_arr[1] << ",";
    cout << test_arr[2] << ",";
    cout << test_arr[3] << ",";
    cout << test_arr[4] << endl;

    return 0;
}

2 容器排序

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

int main()
{
    std::vector<int> test_vec;
    test_vec.push_back(2);
    test_vec.push_back(3);
    test_vec.push_back(5);
    test_vec.push_back(1);
    test_vec.push_back(4);

    std::sort(test_vec.begin(),test_vec.end());

    for(size_t i=0; i<test_vec.size(); ++i)
        std::cout<<test_vec[i]<<std::endl;

    return 0;
}

3 重载比较函数-全局

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

class Person
{
public:
    // default constructor
    Person() : age(0) {}
    Person(int age, std::string name) {
        this->age = age; this->name = name;
    }
    int age;
    std::string name;
};

inline bool operator<(const Person& a, const Person& b) const   //注意此处加const,原博没有导致使用时出错
{
    return a.age < b.age;
}

int main()
{
    std::vector<Person> persons;
    persons.push_back(Person(24,"Calvin"));
    persons.push_back(Person(30,"Benny"));
    persons.push_back(Person(28,"Alison"));

    std::sort(persons.begin(),persons.end());

    for(size_t i=0; i<persons.size(); ++i)
        std::cout<<persons[i].age<<", "<<persons[i].name<<std::endl;

    return 0;
}

4 重载比较函数-成员函数

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

class Person
{
public:
    // default constructor
    Person() : age(0) {}
    Person(int age, std::string name) {
        this->age = age; this->name = name;
    }
    bool operator<(const Person& rhs) const  //注意加const
    {
        return this->age < rhs.age;
    }
    int age;
    std::string name;
};

int main()
{
    std::vector<Person> persons;
    persons.push_back(Person(24,"Calvin"));
    persons.push_back(Person(30,"Benny"));
    persons.push_back(Person(28,"Alison"));

    std::sort(persons.begin(),persons.end());

    for(size_t i=0; i<persons.size(); ++i)
        std::cout<<persons[i].age<<", "<<persons[i].name<<std::endl;

    return 0;
}

5 函数指针

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

class Person
{
public:
    // default constructor
    Person() : age(0) {}
    Person(int age, std::string name) {
        this->age = age; this->name = name;
    }

    int age;
    std::string name;
};

bool Greater(const Person& a, const Person& b)
{
    if(a.age == b.age)
        return a.name < b.name;

    return a.age > b.age;
}

int main()
{
    std::vector<Person> persons;
    persons.push_back(Person(24,"Calvin"));
    persons.push_back(Person(30,"Benny"));
    persons.push_back(Person(30,"Alice"));
    persons.push_back(Person(28,"Alison"));

    std::sort(persons.begin(),persons.end(),Greater);

    for(size_t i=0; i<persons.size(); ++i)
        std::cout<<persons[i].age<<", "<<persons[i].name<<std::endl;

    return 0;
}

sort中使用lambda表达式:

std::sort(persons.begin(), persons.end(), [](const Person &a, const Person &b) { return ((*a).age < (*b).age); });

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值