C++拾遗--bind函数绑定
前言
函数绑定bind函数用于把某种形式的参数列表与已知的函数进行绑定,形成新的函数。这种更改已有函数调用模式的做法,就叫函数绑定。需要指出:bind就是函数适配器。
正文
适配器
#include <iostream>
#include <functional>
using namespace std;
using namespace std::placeholders;
int main()
{
auto fun = [](int *array, int n, int num){
for (int i = 0; i < n; i++)
{
if (array[i] > num)
cout << array[i] << ends;
}
cout << endl;
};
int array[] = { 1, 3, 5, 7, 9 };
//_1,_2 是占位符
auto fun1 = bind(fun, _1, _2, 5);
//等价于调用fun(array, sizeof(array) / sizeof(*array), 5);
fun1(array, sizeof(array) / sizeof(*array));
cin.get();
return 0;
}
运行7 9

本文介绍了C++中的bind函数,作为函数适配器,它用于将参数列表与已知函数绑定,创建新的可调用对象。主要内容包括bind的常见用法、绑定类成员函数及其在减少参数和调整参数顺序上的作用。
最低0.47元/天 解锁文章
1298

被折叠的 条评论
为什么被折叠?



