C++11标准库bind函数应用教程

目录

  • bind 是什么?
  • 举个栗子
    • 1. bind 无参数的普通函数
    • 2. bind 1个参数的普通函数
    • 3. bind 多个参数的普通函数
    • 4. bind 多个参数的普通函数并打乱参数位置
    • 5. bind 类的成员函数
  • 再举个应用栗子

    bind 是什么? bind 顾名思义: 绑定
    通俗来讲呢,可以这么理解有点像函数指针的意思。
    资料上是这么讲的:可以将 bind 函数看做一个通用函数的适配器,它接受一个可调用对象,生成一个新的可以调用对象来“适应”原对象参数列表
    【C++11标准库bind函数应用教程】它一般调用形式:
    // 其中 newCallable 是一个可调用的对象, arg_list 是以逗号分隔的参数列表// 这时我们调用 newCallable,newCallable 就会调用 callable, 并用 arg_list 传递参数auto newCallable = bind(callable, arg_list);

    好了,重点在于 arg_list 里,那么我们如何传入参数呢
    它们是靠这些参数的位置来识别的,形如 _n 之类的, n 是整形, _1 是第一个参数,_2是第二个参数,以此类推。
    而名字 _n 是定义在 placeholders 命名空间中, 而 placeholders 本身又定义在 std 命名空间中, 所以形如:
    using std:: placeholders::_1
    接下来,我们举例几个列子

    举个栗子 bind 是在头文件 #include 中, 首先要包含它。

    1. bind 无参数的普通函数
    #include #include // 包含此头文件// 普通函数void Fun(){ std::cout << "I am Fun!" << std::endl; }// 主函数int main(){ auto fun = std::bind(Fun); // 适配 Fun 函数并返回一个可调用的对象 fun(); return 0; }

    调试结果:
    C++11标准库bind函数应用教程
    文章图片


    2. bind 1个参数的普通函数
    #include #include // 包含此头文件// 普通函数void Fun(int a){ std::cout << "I am Fun! a = " << a <
    调试结果:
    C++11标准库bind函数应用教程
    文章图片


    3. bind 多个参数的普通函数
    #include #include // 包含此头文件// 普通函数int Fun(int a, int b){ return a - b; }// 主函数int main(){ auto fun = std::bind(Fun, std::placeholders::_1, std::placeholders::_2); std::cout << fun(5, 2) << std::endl; return 0; }

    调试结果:
    C++11标准库bind函数应用教程
    文章图片


    4. bind 多个参数的普通函数并打乱参数位置
    #include #include // 包含此头文件// 普通函数int Fun(int a, int b){ return a - b; }// 主函数int main(){ auto fun1 = std::bind(Fun, std::placeholders::_1, std::placeholders::_2); auto fun2 = std::bind(Fun, std::placeholders::_2, std::placeholders::_1); std::cout << fun1(5, 2) << std::endl; std::cout << fun1(5, 2) << std::endl; return 0; }

    调试结果:
    C++11标准库bind函数应用教程
    文章图片


    5. bind 类的成员函数
    #include #include // 包含此头文件class MyClass {public: MyClass() {} ~MyClass() {}public: void printInfo() {std::cout << "MyClass Info." << std::endl; }}; // 主函数int main(){ MyClass A; auto fun = std::bind(&MyClass::printInfo, A); fun(); return 0; }

    调试结果:
    C++11标准库bind函数应用教程
    文章图片


    再举个应用栗子
    #include #include // 包含此头文件typedef std::function CallbackType; // A 类class MyClassA {public: void regeditCallBack(CallbackType fun) { _callback_fun = fun; } void printInfoA(int d) { _callback_fun(d); }private: CallbackType _callback_fun; }; // B 类class MyClassB {public: void printInfoB(int d) {std::cout << d << std::endl; }}; // 主函数int main(){ MyClassB B; auto funB = std::bind(&MyClassB::printInfoB, B, std::placeholders::_1); MyClassA A; A.regeditCallBack(funB); A.printInfoA(1); return 0; }

    调试结果:
    C++11标准库bind函数应用教程
    文章图片

    到此这篇关于C++11标准库bind函数应用教程的文章就介绍到这了,更多相关C++11 bind函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

      推荐阅读