zoukankan      html  css  js  c++  java
  • 0801-----C++Primer听课笔记----------C++11新特性 function 和 bind 的简单使用

    1.function 和 函数指针

      1.1 function有函数指针的功能,但是使用起来明显比函数指针更加灵活和方便。

      1.2 函数指针和function的用法实例。

        1.2.1 函数指针首先要清楚函数指针的类型,如void (*)(int, char)等,然后声明一函数指针变量直接调用即可。

    #include <iostream>
    using namespace std;
    /*
     * 函数指针的用法
     */
    
    void test(int i,double j){
        cout << i << " " << j << endl;
    }
    
    int main(int argc, const char *argv[])
    {
        void (*fp)(int, double);
        fp = test;
        fp(3, 2.3);

        1.2.2 function 和函数指针类型,需要函数的类型,定义一个该类的对象,用法和函数指针相同。

    #include <iostream>
    #include <functional>
    using namespace std;
    /*
     * c++11 function
     *
     */
    
    void test(int i,double j){
        cout << i << " " << j << endl;
    }
    
    int main(int argc, const char *argv[])
    {
        function<void (int, double)> fp;
        fp = test;
        fp(3, 2.3);
        return 0;
    }

    2.function 和 bind 的简单使用

      2.1 bind函数用来将一个function对象和一个函数实现绑定,这里使用了占位符,这里占位符所占的位置是调用的时候的位置。

      2.2 程序示例。

    #include <iostream>
    #include <string>
    #include <functional>
    using namespace std;
    
    /*
     * bind 实现function的任意绑定
     *
     */
    
    void test(int i, double d, const string &s){
        cout << "i = " << i << " d = " << d << " s = " << s << endl;
    }
    
    int main(int argc, const char *argv[])
    {
        //1.void(*)(int, double)
        function <void (int, double)> fp;
        string s = "fp...";
        fp = bind(&test, std::placeholders::_1, std::placeholders::_2, s);
        fp(1, 1.1);
    
        //2. void(*)(double, int, const string &)
        function <void (double, int, const string &)> fp2;
        fp2 = bind(&test, std::placeholders::_2, std::placeholders::_1, std::placeho                                lders::_3);
        fp2(2.2, 2, "fp2...");
    
        //3. void(*)(const string &, int)
        function <void (const string &)> fp3;
        fp3 = bind(&test, 3, 3.3, std::placeholders::_1);
        fp3("fp3...");
    
        //4. void(*)(const string &, int , double)
        function <void (const string &, int, double)> fp4;
        fp4 = bind(&test, std::placeholders::_2, std::placeholders::_3, std::placeho                                lders::_1);
        fp4("fp4...", 4, 4.4);
    
        //5. void(*)(int)
        function <void (int)> fp5;
        fp5 = bind(&test, std::placeholders::_1, 5.5, "fp5...");
        fp5(5);
    
        //6. void(*)(const string &)
        function <void (const string &)> fp6;
        fp6 = bind(&test, 6, 6.6, std::placeholders::_1);
        fp6("fp6...");
    
        //7. void(*)()
        function <void ()> fp7;
        fp7 = bind(&test, 7, 7.7, "fp7...");
        fp7();
    
    
        return 0;
    }

      2.3 function和bind的使用示例。

    #include <iostream>
    #include <string>
    #include <vector>
    #include <functional>
    using namespace std;
    /*
     * c++11 function
     *
     */
    
    // void (*)()
    void test(){
        cout << "test... " << endl;
    }
    
    class Test{
        public:
    
            //void (*)()
            static void test_static(){
                cout << "test static ..." << endl;
            }
    
            //void (Test::*)() //含有隐式参数 Test *
            void test2(){
                cout << "test2... In Test" << endl;
            }
    
            //void (Test::*)(int)
            void test3(int i){
                cout << i << " test3 ... In Test" << endl;
            }
    };
    int main(int argc, const char *argv[])
    {
        function<void ()> fp;
        fp = test;
        fp();
    
        fp = Test::test_static; // static 函数名包含类名
        fp();
    
        Test t;
        fp = bind(&Test::test2, &t); //绑定一个对象的指针
        fp();
    
        fp = bind(&Test::test3, &t, 3);
        fp();
    
        // test3  转换成 void (*)(int)类型的
        function<void (int)> fp2;
        fp2 = bind(&Test::test3, &t, std::placeholders::_1);
        fp2(2000);
    
        return 0;
    }
    

      

  • 相关阅读:
    Java核心技术点之动态代理
    一起写一个Android图片加载框架
    Java核心技术点之注解
    Android中的Intent Filter匹配规则介绍
    阿里客户端工程师试题简析——Android应用的闪退(crash)分析
    Android开发之高效加载Bitmap
    Java NIO:浅析I/O模型
    深入探索Android中的Handler
    git常用命令
    SM2
  • 原文地址:https://www.cnblogs.com/monicalee/p/3885712.html
Copyright © 2011-2022 走看看