C++11中的bind

ops/2025/2/5 11:39:53/

官方文档对于bind接口的概述解释:Bind function arguments

在C++11中,std::bind 是一个非常有用的工具,用于将函数、成员函数或函数对象与特定的参数绑定在一起,生成一个新的可调用对象。std::bind 可以用于部分应用函数参数、改变参数的顺序、绑定成员函数等

#include <functional> // 需要包含这个头文件auto new_callable = std::bind(callable, arg1, arg2, ..., argN);
  • callable 可以是函数、函数指针、成员函数指针、函数对象等。

  • arg1, arg2, ..., argN 是传递给 callable 的参数。这些参数可以是具体的值,也可以是占位符 std::placeholders::_1, std::placeholders::_2, 等等。

示例
1. 绑定普通函数
#include <iostream>
#include <functional>void print_sum(int a, int b) {std::cout << a + b << std::endl;
}int main() {auto f = std::bind(print_sum, 10, 20);f(); // 输出 30return 0;
}

在这个例子中,std::bindprint_sum 函数与参数 1020 绑定在一起,生成了一个新的可调用对象 f。调用 f() 时,相当于调用了 print_sum(10, 20)

2. 使用占位符
#include <iostream>
#include <functional>void print_sum(int a, int b) {std::cout << a + b << std::endl;
}int main() {using namespace std::placeholders; // 引入占位符auto f = std::bind(print_sum, _1, _2);f(10, 20); // 输出 30auto g = std::bind(print_sum, _2, _1);g(10, 20); // 输出 30,参数顺序被交换return 0;
}

在这个例子中,_1_2 是占位符,表示在调用 fg 时传递的第一个和第二个参数。

3. 绑定成员函数
#include <iostream>
#include <functional>class MyClass {
public:void print_sum(int a, int b) {std::cout << a + b << std::endl;}
};int main() {MyClass obj;auto f = std::bind(&MyClass::print_sum, &obj, 10, 20);f(); // 输出 30using namespace std::placeholders;auto g = std::bind(&MyClass::print_sum, &obj, _1, _2);g(10, 20); // 输出 30return 0;
}

在这个例子中,std::bind 绑定了 MyClass 的成员函数 print_sum,并且需要传递一个对象指针 &obj作为第一个参数。

4. 绑定函数对象
#include <iostream>
#include <functional>struct Add {int operator()(int a, int b) const {return a + b;}
};int main() {Add add;auto f = std::bind(add, 10, 20);std::cout << f() << std::endl; // 输出 30using namespace std::placeholders;auto g = std::bind(add, _1, _2);std::cout << g(10, 20) << std::endl; // 输出 30return 0;
}

在这个例子中,std::bind 绑定了一个函数对象 add,并生成了一个新的可调用对象 fg

5.线程池

基于bind的作用,当我们在设计一些线程池,或者任务池的时候,就可以将将任务池中的任务设置为函数类型,函数的参数由添加任务者直接使用bind进行适配绑定设置,而任务池中的任务被处理,只需要取出一个个的函数进行执行即可。

这样做有个好处就是,这种任务池在设计的时候,不用考虑都有哪些任务处理方式了,处理函数该如何设计,有多少个什么样的参数,这些都不用考虑了,降低了代码之间的耦合度。

#include <iostream>
#include <functional>
#include <cstring>
#include <vector>void Print(const std::string& str, int x)
{std::cout << str << "--->" << x << std::endl;
}int main()
{using task_t = std::function<void()>;std::vector<task_t> _task;_task.push_back(std::bind(Print, "wuxu", 666));_task.push_back(std::bind(Print, "cdd", 888));_task.push_back(std::bind(Print, "Mudu", 666));for(auto& f : _task)f();}


http://www.ppmy.cn/ops/155850.html

相关文章

Vue3 完整学习笔记 - 第二部分

Vue3 完整学习笔记 - 第二部分 2. Vue3 响应式系统深入 2.1 ref 深入理解 重点掌握&#xff1a; ref 的工作原理基本类型和对象类型的处理差异template 中的自动解包 核心示例&#xff1a; <template><div><!-- 模板中自动解包&#xff0c;无需 .value --…

基于springboot+vue的中药实验管理系统(源码+数据库+文档)

中药实验管理系统 目录 基于springbootvue的中药实验管理系统 一、前言 二、系统设计 三、系统功能设计 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#xff1a;✌️大厂码农|毕设布道师&#xff0c;…

51c视觉~CV~合集10

我自己的原文哦~ https://blog.51cto.com/whaosoft/13241694 一、CV创建自定义图像滤镜 热图滤镜 这组滤镜提供了各种不同的艺术和风格化光学图像捕捉方法。例如&#xff0c;热滤镜会将图像转换为“热图”&#xff0c;而卡通滤镜则提供生动的图像&#xff0c;这些图像看起来…

Spring Boot基本项目结构

要写一个Spring Boot 项目对于新手小白来说&#xff0c;首先要了解Spring Boot 的基本架构&#xff0c;学会如何创建一个简单的spring boot项目。 springboot 基于maven做的&#xff08;前提保证maven是装好并且IDEA配置好的&#xff09;&#xff08;面向接口编程&#xff09;…

深度解析近期爆火的 DeepSeek

最近&#xff0c;AI 领域有个名字频繁出现在大众视野 ——DeepSeek&#xff0c;它的火爆程度就像一颗投入平静湖面的巨石&#xff0c;激起千层浪。今天&#xff0c;咱们就来深入了解一下这个 “AI 新星”。 官网&#xff1a;DeepSeek - 探索未至之境 DeepSeek 是什么 DeepSeek…

试用ChatGPT开发一个大语言模型聊天App

参考官方文档&#xff0c;安装android studio https://developer.android.com/studio/install?hlzh-cn 参考这个添加permission权限&#xff1a; https://blog.csdn.net/qingye_love/article/details/14452863 参考下面链接完成Android Studio 给项目添加 gradle 依赖 ht…

git push到远程仓库时无法推送大文件

一、错误 remote: Error: Deny by project hooks setting ‘default’: size of the file ‘scientific_calculator’, is 164 MiB, which has exceeded the limited size (100 MiB) in commit ‘4c91b7e3a04b8034892414d649860bf12416b614’. 二、原因 本地提交过大文件&am…

Unity游戏(Assault空对地打击)开发(6) 鼠标光标的隐藏

前言 鼠标光标在游戏界面太碍眼了&#xff0c;要隐藏掉。 详细操作 新建一个脚本HideCursor&#xff0c;用于隐藏光标。 写入以下代码。 意义&#xff1a;游戏开始自动隐藏光标&#xff0c;按Esc&#xff08;显示<-->隐藏&#xff09;。 using System.Collections; using…