c++中std::bind怎么用_std::bind函数适配器使用方法详解

std::bind是C++11引入的函数适配器,用于绑定可调用对象与参数生成新可调用对象,需包含<functional>头文件;通过占位符_1、_2等指定延迟传入的参数,支持参数预设、重排与重复使用;可绑定普通函数、成员函数(需传对象指针和成员函数指针)、lambda等;常用于STL算法中构造回调函数,如结合std::for_each实现固定日志级别输出;尽管lambda更简洁,std::bind在复杂参数绑定场景仍具价值。

c++中std::bind怎么用_std::bind函数适配器使用方法详解

std::bind 是 C++11 引入的一个函数适配器,用于将可调用对象(如函数、函数指针、成员函数、lambda 表达式等)与其参数进行绑定,生成一个新的可调用对象。它在需要延迟调用、参数预设或回调函数的场景中非常有用。

基本语法与头文件

使用 std::bind 需要包含头文件:

#include <functional>

其基本语法如下:

std::bind(callable, arg1, arg2, ...)

其中:

  • callable:可调用对象,比如函数名、函数对象、成员函数指针等。
  • arg1, arg2, …:绑定到 callable 的参数,可以是具体值,也可以是占位符(placeholder)。

占位符的使用

占位符定义在 std::placeholders 命名空间中,例如 _1_2_3 等,表示将来调用时传入的第 1、第 2、第 3 个参数。

示例:

立即学习C++免费学习笔记(深入)”;

c++中std::bind怎么用_std::bind函数适配器使用方法详解

阿里云-虚拟数字人

阿里云-虚拟数字人是什么? …

c++中std::bind怎么用_std::bind函数适配器使用方法详解2

查看详情 c++中std::bind怎么用_std::bind函数适配器使用方法详解

#include <functional> #include <iostream>  void print_sum(int a, int b) {     std::cout << a + b << std::endl; }  int main() {     auto bound_func = std::bind(print_sum, _1, 10);     bound_func(5);  // 输出 15,相当于 print_sum(5, 10) }

这里 _1 表示调用 bound_func 时传入的第一个参数,而 10 被固定为第二个参数。

绑定成员函数

绑定类的成员函数时,第一个参数必须是对象或指向对象的指针(或引用),然后是成员函数指针,再后是参数。

示例:

立即学习C++免费学习笔记(深入)”;

c++中std::bind怎么用_std::bind函数适配器使用方法详解

阿里云-虚拟数字人

阿里云-虚拟数字人是什么? …

c++中std::bind怎么用_std::bind函数适配器使用方法详解2

查看详情 c++中std::bind怎么用_std::bind函数适配器使用方法详解

#include <functional> #include <iostream>  struct Calculator {     int add(int a, int b) {         return a + b;     } };  int main() {     Calculator calc;     auto bound_add = std::bind(&amp;Calculator::add, &calc, _1, _2);     int result = bound_add(3, 4);  // 返回 7     std::cout << result << std::endl; }

注意:&amp;Calculator::add 是成员函数指针,std::bind(callable, arg1, arg2, ...)0 是对象地址,_1_2 对应成员函数的两个参数。

参数重排与重复使用

通过占位符,可以重新排列参数顺序,甚至重复使用同一个参数。

示例:

立即学习C++免费学习笔记(深入)”;

c++中std::bind怎么用_std::bind函数适配器使用方法详解

阿里云-虚拟数字人

阿里云-虚拟数字人是什么? …

c++中std::bind怎么用_std::bind函数适配器使用方法详解2

查看详情 c++中std::bind怎么用_std::bind函数适配器使用方法详解

void print_values(int x, int y, int z) {     std::cout << x << ", " << y << ", " << z << std::endl; }  auto func = std::bind(print_values, _2, _1, _1); func(10, 20);  // 输出:20, 10, 10

这里调用时传入 (10, 20),对应 _1=10, _2=20,最终参数顺序变为 (20, 10, 10)。

与 STL 算法结合使用

std::bind 常用于配合 STL 算法,如 std::bind(callable, arg1, arg2, ...)3、std::bind(callable, arg1, arg2, ...)4 等。

示例:

立即学习C++免费学习笔记(深入)”;

c++中std::bind怎么用_std::bind函数适配器使用方法详解

阿里云-虚拟数字人

阿里云-虚拟数字人是什么? …

c++中std::bind怎么用_std::bind函数适配器使用方法详解2

查看详情 c++中std::bind怎么用_std::bind函数适配器使用方法详解

#include <vector> #include <algorithm> #include <functional> #include <iostream>  void log(const std::string& level, const std::string& msg) {     std::cout << "[" << level << "] " << msg << std::endl; }  int main() {     std::vector<std::string> messages = {"Error occurred", "File saved"};      auto logger = std::bind(log, "INFO", _1);     std::for_each(messages.begin(), messages.end(), logger); }

输出:

[INFO] Error occurred [INFO] File saved

这里将 std::bind(callable, arg1, arg2, ...)5 固定作为第一个参数,_1 接收容器中的每个消息。

基本上就这些。std::bind 提供了灵活的方式来封装函数调用逻辑,虽然 C++11 之后 lambda 更加简洁常用,但在某些复杂绑定场景下,std::bind 依然有其价值。掌握它有助于理解函数对象和回调机制的本质。

go 回调函数 ai c++ ios 排列 red 命名空间 封装 成员函数 include 回调函数 Lambda 指针 对象 transform 算法

上一篇
下一篇