STL Sort的五種使用樣本

來源:互聯網
上載者:User

Sort是一種高效的排序演算法,常使用的五種樣本如下。
範例程式碼採用編譯器vs2012,任何一款支援c++11標準的編譯器都可以運行。

包含標頭檔

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

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;    }}

調用
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;    }}

調用
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;    }}

調用
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;    }}

調用
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;    }}

調用
SORT_5::Test();

結果
3 4 1 2 5

6、總結
當操作對象已重載操作符時,可以直接sort
當排序演算法是局部臨時演算法時,可以採用lambda
當排序演算法可提出共用時,可採用bind+function

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.