std :: function和std :: bind:他们是什么&何时应该使用? [英] std::function and std::bind: what are they & when they should be used?

查看:115
本文介绍了std :: function和std :: bind:他们是什么&何时应该使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道什么是函子,什么时候使用stl algos。但不明白他在这个主题中的意思:

I know what functors are and when to use them with stl algos. but didn't understand what did he mean in this topic:

C ++ 11FAQ

任何人都可以解释 std :: bind std :: function 是否应该使用newbie的一些示例?

Can any one explain what std::bind and std::function are, when it should be used, some examples for newbie?

推荐答案

std :: bind 适用于部分功能应用程序< a>。

std::bind is for partial function application.

也就是说,假设你有一个函数对象 f ,它有3个参数:

That is, suppose you have a function object f which takes 3 arguments:

f(a,b,c);

您需要一个只包含两个参数的新函数对象,定义如下:

You want a new function object which only takes two arguments, defined as:

g(a,b) := f(a, 4, b);

g f :中间参数已经被指定,还有两个要离开。

g is a "partial application" of the function f: the middle argument has already been specified, and there are two left to go.

使用 std :: bind 获取 g

auto g = bind(f, _1, 4, _2);

这比实际写一个functor类要简单。

This is more concise than actually writing a functor class to do it.

您链接到的文章还有其他示例。你通常使用它,当你需要传递函子到一些算法。您有一个函数或函数几乎执行所需的作业,但是比算法使用的函数或函数更可配置(即具有更多参数)。所以你绑定参数到一些参数,并留下算法的其余部分填充:

There are further examples in the article you link to. You generally use it when you need to pass a functor to some algorithm. You have a function or functor that almost does the job you want, but is more configurable (i.e. has more parameters) than the algorithm uses. So you bind arguments to some of the parameters, and leave the rest for the algorithm to fill in:

// raise every value in vec to the power of 7
std::transform(vec.begin(), vec.end(), some_output, std::bind(std::pow, _1, 7));

这里, pow 提高到任何权力,但我们关心的是提高到7的权力。

Here, pow takes two parameters and can raise to any power, but all we care about is raising to the power of 7.

作为不是部分功能的偶尔使用应用程序 bind 也可以重新排序函数的参数:

As an occasional use that isn't partial function application, bind can also re-order the arguments to a function:

auto memcpy_with_the_parameters_in_the_right_flipping_order = bind(memcpy, _2, _1, _3);

我不建议使用它,只是因为你不喜欢API,例如因为:

I don't recommend using it just because you don't like the API, but it has potential practical uses for example because:

not2(bind(less<T>, _2, _1));

是一个小于或等于功能(假设总订单,blah blah)。此示例通常不是必需的,因为已经有 std :: less_equal (它使用< = 运算符而不是< ,所以如果他们不一致,那么你可能需要这个,你可能还需要访问类的作者与一个嘲笑)。如果你使用一种功能性的编程风格,这是一种转变。

is a less-than-or-equal function (assuming a total order, blah blah). This example normally isn't necessary since there already is a std::less_equal (it uses the <= operator rather than <, so if they aren't consistent then you might need this, and you might also need to visit the author of the class with a cluestick). It's the sort of transformation that comes up if you're using a functional style of programming, though.

这篇关于std :: function和std :: bind:他们是什么&何时应该使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆