• std::mem_fn


    std::mem_fn

    1. 不支持的场景

    1.1 不支持全局函数

    1.2 不支持类protected访问权限的成员(函数或数据)

    1.3 不支持类private访问权限的成员(函数或数据)

    2. 支持的场景

    2.1 传入类对象

    2.2 传入引用对象

    2.3 传入右值

    2.4 传入对象指针

    2.5 传入智能指针std::shared_ptr

    2.6 传入智能指针std::unique_ptr

    2.7 传入派生类对象

    2.8 带参数的成员函数

    3. 场景示例代码

    #include <functional>
    #include <iostream>
    
    int Add(int a, int b)
    {
        return a + b;
    }
    
    class Age
    {
    public:
        Age(int default = 12) : m_age(default)
        {}
    
        bool compare(const Age& t) const
        {
            return m_age < t.m_age;
        }
    
        void print() const
        {
            std::cout << m_age << ' ';
        }
    
        int m_age;
    
    protected:
        void add(int n)
        {
            m_age += n;
        }
    
    private:
        void sub(int m)
        {
            m_age -= m;
        }
    };
    
    class DerivedAge : public Age
    {
    public:
        DerivedAge(int default = 22) : Age(default)
        {}
    };
    
    int main(int argc, char* argv[])
    {
        // 0.不支持的示例
        {
            // 1.不支持全局函数
            // auto globalFunc = std::mem_fn(Add); // ERROR: 语法无法通过
            // 2.不支持类protected访问权限的函数
            // auto addFunc = std::mem_fn(&Age::add); // ERROR: 语法无法通过
            // 3.不支持类private访问权限的函数
            // auto subFunc = std::mem_fn(&Age::sub); // ERROR: 语法无法通过
        }
        // 1.成员函数示例
        {
            auto memFunc = std::mem_fn(&Age::print);
    
            // 方式一:传入类对象
            Age obja{ 18 };
            memFunc(obja);
    
            Age& refObj = obja;
            refObj.m_age = 28;
            // 方式二:传入引用对象
            memFunc(refObj);
    
            // 方式三:传入右值
            Age objb{ 38 };
            memFunc(std::move(objb));
    
            // 方式四:传入对象指针
            Age objc{ 48 };
            memFunc(&objc);
    
            // 方式五:传入智能指针std::shared_ptr
            std::shared_ptr<Age> pAge1 = std::make_shared<Age>(58);
            memFunc(pAge1);
    
            // 方式六:传入智能指针std::unique_ptr
            std::unique_ptr<Age> pAge2 = std::make_unique<Age>(68);
            memFunc(pAge2);
    
            // 方式七:传入派生类对象
            DerivedAge aged{ 78 };
            memFunc(aged);
            
            // 方式八:带参数成员函数
            auto memFuncWithParams = std::mem_fn(&Age::compare);
            std::cout << memFuncWithParams(Age{ 25 }, Age{ 35 }) << std::endl;
        }
    
        std::cout << std::endl;
    
        // 2.成员变量示例
        {
            auto memData = std::mem_fn(&Age::m_age);
    
            // 方式一:传入类对象
            Age obja{ 19 };
            std::cout << memData(obja) << ' ';
    
            Age& refObj = obja;
            refObj.m_age = 29;
            // 方式二:传入引用对象
            std::cout << memData(refObj) << ' ';
    
            // 方式三:传入右值
            Age objb{ 39 };
            std::cout << memData(std::move(objb)) << ' ';
    
            // 方式四:传入对象指针
            Age objc{ 49 };
            std::cout << memData(&objc) << ' ';
    
            // 方式五:传入智能指针std::shared_ptr
            std::shared_ptr<Age> pAge1 = std::make_shared<Age>(59);
            std::cout << memData(pAge1) << ' ';
    
            // 方式六:传入智能指针std::unique_ptr
            std::unique_ptr<Age> pAge2 = std::make_unique<Age>(69);
            std::cout << memData(pAge2) << ' ';
    
            // 方式七:传入派生类对象
            DerivedAge aged{ 79 };
            std::cout << memData(aged) << ' ';
        }
    
        return 0;
    }
    
    /* run output:
    18 28 38 48 58 68 78 1
    19 29 39 49 59 69 79
    */
    

    4. 应用示例

    4.1 以前写法

    4.2 使用std::mem_fn写法

    4.3 示例代码

    #include <functional>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    class Age
    {
    public:
        Age(int v) : m_age(v)
        {}
    
        bool compare(const Age& t) const
        {
            return m_age < t.m_age;
        }
    
        void print() const
        {
            std::cout << m_age << ' ';
        }
    
        int m_age;
    };
    
    bool compare(const Age& t1, const Age& t2)
    {
        return t1.compare(t2);
    }
    
    int main(int argc, char* argv[])
    {
        // 以前写法
        {
            std::vector<Age> ages{ 1, 7, 19, 27, 39, 16, 13, 18 };
            std::sort(ages.begin(), ages.end(), [&](const Age& objA, const Age& objB) {
                return compare(objA, objB);
                });
            for (auto item : ages)
            {
                item.print();
            }
            std::cout << std::endl;
        }
        // 利用std::mem_fn写法
        {
            std::vector<Age> ages{ 100, 70, 290, 170, 390, 160, 300, 180 };
            std::sort(ages.begin(), ages.end(), std::mem_fn(&Age::compare));
            std::for_each(ages.begin(), ages.end(), std::mem_fn(&Age::print));
            std::cout << std::endl;
        }
    
        return 0;
    }
    
    /* run output:
    1 7 13 16 18 19 27 39
    70 100 160 170 180 290 300 390
    */
    
  • 相关阅读:
    luogu P1019 单词接龙
    luogu P4137 Rmq Problem / mex
    Virtualbox 修改硬盘的序列号等信息 例
    httpHandlers path="*.sky"
    Oracle告Google输了
    %STSADM% -o addsolution -filename AEMediaPlayerWebpart.wsp
    placeholder
    String强制转换为Date,freemarker标签里date数据的显示问题
    eclipse配置JDK和设置编译版本的几种方法
    httpd 系统错误 无法启动此程序,因为计算机中丢失VCRUNTIME140.dll
  • 原文地址:https://www.cnblogs.com/Braveliu/p/16875374.html
Copyright © 2020-2023  润新知