stl sort的五种用法

Sort是一种高效的排序算法,常使用的五种示例如下。
示例代码采用编译器vs2012,任何一款支持c++11标准的编译器都可以运行。

包含头文件

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
   
   
  • 1
  • 2
  • 3
  • 4

1、重载操作符
重载对应操作符<、>、<=、>=来实现排序
代码

namespace SORT_1
{
    //重载操作符
    struct StTest
    {
        StTest(int nInput)
            : nVal(nInput)
        {}

        bool operator<(const StTest& other) const
        {
            return (this->nVal < other.nVal);
        }

        bool operator<=(const StTest& other) const
        {
            return (this->nVal <= other.nVal);
        }

        bool operator>(const StTest& other) const
        {
            return (this->nVal > other.nVal);
        }

        bool operator>=(const StTest& other) const
        {
            return (this->nVal >= other.nVal);
        }

        int nVal;
    };

    void Test()
    {
        std::vector<StTest> vTest;
        vTest.push_back(StTest(3));
        vTest.push_back(StTest(2));
        vTest.push_back(StTest(5));
        vTest.push_back(StTest(4));
        vTest.push_back(StTest(1));

        //从小到大排序
        std::sort(vTest.begin(), vTest.end(), std::less<StTest>());
        for (auto elem : vTest)
        {
            std::cout<<(elem.nVal)<<" ";
        }
        std::cout<<std::endl;

        //从大到小排序
        std::sort(vTest.begin(), vTest.end(), std::greater<StTest>());
        for (auto elem : vTest)
        {
            std::cout<<(elem.nVal)<<" ";
        }
        std::cout<<std::endl;
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

调用
SORT_1::Test();

结果
1 2 3 4 5
5 4 3 2 1

2、函数指针
示例代码

namespace SORT_2
{
    //函数指针
    struct StTest
    {
        StTest(int nInput)
            : nVal(nInput)
        {}

        int nVal;
    };

    bool SortFunc(const StTest& lVal, const StTest& rVal)
    {
        return (lVal.nVal < rVal.nVal);
    }

    void Test()
    {
        std::vector<StTest> vTest;
        vTest.push_back(StTest(3));
        vTest.push_back(StTest(2));
        vTest.push_back(StTest(5));
        vTest.push_back(StTest(4));
        vTest.push_back(StTest(1));

        std::sort(vTest.begin(), vTest.end(), SortFunc);
        for (auto elem : vTest)
        {
            std::cout<<(elem.nVal)<<" ";
        }
        std::cout<<std::endl;
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

调用
SORT_2::Test();

结果
1 2 3 4 5

3、仿函数
当排序需要传参时,我们可以用仿函数。
代码示例

namespace SORT_3
{
    //仿函数
    struct StTest
    {
        StTest(int nInput)
            : nVal(nInput)
        {}

        int nVal;
    };

    //求模后排序
    class CModuleFunc
    {
    public:
        CModuleFunc(int nTest)
            : m_nTest(nTest)
        {}

        bool operator()(const StTest& lVal, const StTest& rVal)
        {
            if (m_nTest <= 0) {return false;}

            int nlModule = (lVal.nVal%m_nTest);
            int nrModule = (rVal.nVal%m_nTest);
            return (nlModule < nrModule);
        }

    private:
        int m_nTest;
    };

    void Test()
    {
        std::vector<StTest> vTest;
        vTest.push_back(StTest(3));
        vTest.push_back(StTest(2));
        vTest.push_back(StTest(5));
        vTest.push_back(StTest(4));
        vTest.push_back(StTest(1));

        std::sort(vTest.begin(), vTest.end(), CModuleFunc(3));
        for (auto elem : vTest)
        {
            std::cout<<(elem.nVal)<<" ";
        }
        std::cout<<std::endl;
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

调用
SORT_3::Test();

结果
3 4 1 2 5

4、bind+function
c++11引入了bind和function,我们可以简化一下仿函数写法。

代码示例

namespace SORT_4
{
    //bind+function
    struct StTest
    {
        StTest(int nInput)
            : nVal(nInput)
        {}

        int nVal;
    };

    //求模后排序
    bool ModuleFunc(const StTest& lVal, const StTest& rVal, int nTest)
    {
        if (nTest <= 0) {return false;}

        int nlModule = (lVal.nVal%nTest);
        int nrModule = (rVal.nVal%nTest);
        return (nlModule < nrModule);
    }

    void Test()
    {
        std::vector<StTest> vTest;
        vTest.push_back(StTest(3));
        vTest.push_back(StTest(2));
        vTest.push_back(StTest(5));
        vTest.push_back(StTest(4));
        vTest.push_back(StTest(1));

        //pFnc类型:std::function<bool (const StTest&, const StTest&)>
        auto pFunc = std::bind(&ModuleFunc, std::placeholders::_1, std::placeholders::_2, 3);
        std::sort(vTest.begin(), vTest.end(), pFunc);
        for (auto elem : vTest)
        {
            std::cout<<(elem.nVal)<<" ";
        }
        std::cout<<std::endl;
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

调用
SORT_4::Test();

结果
3 4 1 2 5

5、lambda
C++ 11引入了lambda,可以创建匿名函数来简化排序。
代码

namespace SORT_5
{
    //lambda
    struct StTest
    {
        StTest(int nInput)
            : nVal(nInput)
        {}

        int nVal;
    };

    void Test()
    {
        std::vector<StTest> vTest;
        vTest.push_back(StTest(3));
        vTest.push_back(StTest(2));
        vTest.push_back(StTest(5));
        vTest.push_back(StTest(4));
        vTest.push_back(StTest(1));

        //采用lambda来进行排序
        int nTest = 3;
        std::sort(vTest.begin(), vTest.end(), [&nTest](const StTest& lVal, const StTest& rVal){
            if (nTest <= 0) {return false;}

            int nlModule = (lVal.nVal%nTest);
            int nrModule = (rVal.nVal%nTest);
            return (nlModule < nrModule);
        });

        for (auto elem : vTest)
        {
            std::cout<<(elem.nVal)<<" ";
        }
        std::cout<<std::endl;
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

调用
SORT_5::Test();

结果
3 4 1 2 5

6、总结
当操作对象已重载操作符时,可以直接sort
当排序算法是局部临时算法时,可以采用lambda
当排序算法可提出共用时,可采用bind+function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值