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

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

问题描述

我知道什么是函子以及何时将其与std算法一起使用,但是我还不了解Stroustrup在

I know what functors are and when to use them with std algorithms, but I haven't understood what Stroustrup says about them in the C++11 FAQ.

谁能解释什么时候应该使用std::bindstd::function,并为新手提供一些示例?

Can anyone explain what std::bind and std::function are, when they should be used, and give some examples for newbies?

推荐答案

std::bind用于部分函数应用.

也就是说,假设您有一个带有3个参数的函数对象f:

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);

这比实际编写函子类来做到这一点更为简洁.

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));

是一个小于或等于函数(假设总阶,等等).由于已经有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天全站免登陆