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

      

  • 相关阅读:
    感到工作效率低?是因为你缺少这套“外贸企业邮箱”办公软件
    用企业邮箱的好处,企业邮箱手机微信使用
    公司企业邮箱注册,公司为什么用企业邮箱?
    中小企业公司邮箱一般用哪个?申请企业邮箱的流程
    公司企业邮箱注册,企业邮箱怎么办理
    array_replace — 使用传递的数组替换第一个数组的元素
    array_replace_recursive — 使用传递的数组递归替换第一个数组的元素
    array_reduce — 用回调函数迭代地将数组简化为单一的值
    array_rand — 从数组中随机取出一个或多个单元
    array_push — 将一个或多个单元压入数组的末尾(入栈)
  • 原文地址:https://www.cnblogs.com/monicalee/p/3885712.html
Copyright © 2011-2022 走看看