c++ std::bind的用法
时间: 2024-05-30 16:06:06 浏览: 14
std::bind是一个非常实用的函数对象适配器,它可以把一个函数对象或者一个函数指针,以及这个函数对象或者函数指针需要的参数,绑定在一起,返回一个新的函数对象,这个新的函数对象只需要传递新的参数,就可以调用原始的函数对象或者函数指针了。这种技术被称为柯里化(Currying),是一种在数学和计算机科学中广泛使用的技术。
以下是std::bind的示例代码:
```
#include <iostream>
#include <functional>
void foo(int a, int b, int c) {
std::cout << "a=" << a << ", b=" << b << ", c=" << c << std::endl;
}
int main() {
auto bind_foo = std::bind(foo, std::placeholders::_2, std::placeholders::_1, 100);
bind_foo(1, 2);
return 0;
}
```
这个示例代码中,我们定义了一个函数foo,它有三个参数a、b和c。然后我们使用std::bind把foo绑定到了一个新的函数对象bind_foo上,并且把foo中第二个参数和第一个参数颠倒了一下,第三个参数绑定为100。最后我们调用bind_foo(1, 2),输出结果为:a=2, b=1, c=100。
相关问题
c++ 中 std::function 和 std::bind的用法
std::function是一个函数对象封装器,能够用于存储任何可调用的对象(函数、Lambda表达式、函数指针等),而std::bind则是一个函数模板,被用来生成一个新的函数对象,通过绑定其参数和调用函数指针、成员指针等来实现对原函数的包装,产生一个可调用的函数对象。
例如,下面的代码用std::bind来绑定成员函数print和成员变量x到一个函数对象f:
```c++
#include <iostream>
#include <functional>
class A {
public:
void print(int num) {
std::cout << "The number is " << num << " and x is " << x << std::endl;
}
int x = 10;
};
int main() {
A a;
std::function<void(int)> f = std::bind(&A::print, &a, std::placeholders::_1);
f(20);
return 0;
}
```
输出结果为:
```c++
The number is 20 and x is 10
```
在这个例子中,我们生成了一个可调用的函数对象f,它绑定到了成员函数print和成员变量x。当我们调用f并传入参数20时,它会输出"The number is 20 and x is 10"。
需要注意的是,std::bind存在一些限制,如无法处理可变参数模板等。在这些情况下,我们可能需要使用Lambda表达式或者将参数改为std::tuple来实现类似的功能。
std::bind 用法
`std::bind` 是 C++11 标准库中的一个函数,它可以用来创建一个函数对象(即可调用对象),并且可以延迟绑定函数的参数。
`std::bind` 的基本语法如下:
```c++
template <class F, class... Args>
std::function<F(Args...)> std::bind(F&& f, Args&&... args);
```
其中,`F` 表示函数类型,`Args` 表示函数参数类型,`f` 表示要绑定的函数,`args` 表示要绑定的参数。
例如,我们可以使用 `std::bind` 来创建一个只有一个参数的函数对象:
```c++
#include <iostream>
#include <functional>
void print(int n) {
std::cout << n << std::endl;
}
int main() {
auto f = std::bind(print, 42);
f(); // 输出 42
return 0;
}
```
在上面的例子中,我们使用 `std::bind` 绑定了一个只有一个参数的函数 `print`,并且将参数设置为 42。然后,我们创建了一个函数对象 `f`,并且调用它,输出了 42。
除了绑定参数之外,`std::bind` 还可以用来调整函数参数的顺序,例如:
```c++
#include <iostream>
#include <functional>
void print(int a, int b) {
std::cout << a << ", " << b << std::endl;
}
int main() {
auto f1 = std::bind(print, 1, 2);
f1(); // 输出 1, 2
auto f2 = std::bind(print, std::placeholders::_2, std::placeholders::_1);
f2(2, 1); // 输出 1, 2
return 0;
}
```
在上面的例子中,我们使用 `std::placeholders::_1` 和 `std::placeholders::_2` 来指定函数参数的位置,然后使用 `std::bind` 绑定了一个函数 `print`,并且调整了参数的顺序,最后创建了一个函数对象 `f2`,并且调用它,输出了 1, 2。
这只是 `std::bind` 的基本用法,它还可以用于更复杂的情况,例如绑定成员函数、绑定多个参数等等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)