bind 是什么?

bind 顾名思义: 绑定

通俗来讲呢,可以这么理解有点像函数指针的意思。

资料上是这么讲的:可以将 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 <functional> 中, 首先要包含它。

1. bind 无参数的普通函数

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

调试结果:

2. bind 1个参数的普通函数

#include <iostream>
#include <functional> // 包含此头文件 // 普通函数
void Fun(int a)
{
std::cout << "I am Fun! a = " << a <<std::endl;
} // 主函数
int main()
{
auto fun = std::bind(Fun, std::placeholders::_1);
fun(5);
return 0;
}

调试结果:

3. bind 多个参数的普通函数

#include <iostream>
#include <functional> // 包含此头文件 // 普通函数
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;
}

调试结果:

4. bind 多个参数的普通函数并打乱参数位置

#include <iostream>
#include <functional> // 包含此头文件 // 普通函数
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;
}

调试结果:

5. bind 类的成员函数

#include <iostream>
#include <functional> // 包含此头文件 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;
}

调试结果:

再举个应用栗子

#include <iostream>
#include <functional> // 包含此头文件 typedef std::function<void(int)> 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;
}

调试结果:

结束

学无止境

最新文章

  1. USACO2.4 The Tamworth Two[模拟]
  2. 设计模式之美:Private Class Data(私有类数据)
  3. SSH-Struts第一弹:ActionSupport类
  4. NeHe OpenGL教程 第十三课:图像字体
  5. 【ContestHunter】【弱省胡策】【Round3】(C)
  6. 表数据转换为insert语句
  7. Windows Azure VM的两种shut down 方式
  8. LeetCode 319
  9. ORACLE添加作业
  10. [c#]asp.net开发微信公众平台(2)多层架构框架搭建和入口实现
  11. phpstorm 下载以及安装
  12. Docker Swarm 配置文件存储
  13. sqoop简单配置与使用
  14. JUC-Condition线程通信
  15. C++语言------顺序表实现,用动态数组的方法
  16. [转载] Activiti Tenant Id 字段释疑
  17. [ 转载 ] get和post的区别
  18. sql 查询不存在左表的数据
  19. Hive的介绍及安装
  20. C++STL学习笔记_(2)deque双端数组知识

热门文章

  1. vue开发中的一些简单骚操作
  2. printf(&quot;%d\n&quot;,printf(&quot;%d&quot;,printf(&quot;%d&quot;,i)));
  3. nrf52810/52832开发板能跑,自己的PCB不能跑的原因
  4. ML2021 | (腾讯)PatrickStar:通过基于块的内存管理实现预训练模型的并行训练
  5. 【JavaSE】Java基础&#183;疑难点汇集
  6. springboot启动流程1
  7. (前端)面试300问之(2)CSS元素居中【水平、垂直、2者同时居中】
  8. Android连接远程数据库的避坑指南
  9. 论文翻译:2020_A Recursive Network with Dynamic Attention for Monaural Speech Enhancement
  10. 水平梯度在sigma坐标对应形式