C ++ Functors - 及其用途 [英] C++ Functors - and their uses

查看:176
本文介绍了C ++ Functors - 及其用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常听到C ++中的functor。

I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful?

推荐答案

一个函子几乎是一个函数只是一个定义operator()的类。这让你创建看起来像一个函数的对象:

A functor is pretty much just a class which defines the operator(). That lets you create objects which "look like" a function:

// this is a functor
struct add_x {
  add_x(int x) : x(x) {}
  int operator()(int y) const { return x + y; }

private:
  int x;
};

// Now you can use it like this:
add_x add42(42); // create an instance of the functor class
int i = add42(8); // and "call" it
assert(i == 50); // and it added 42 to its argument

std::vector<int> in; // assume this contains a bunch of values)
std::vector<int> out(in.size());
// Pass a functor to std::transform, which calls the functor on every element 
// in the input sequence, and stores the result to the output sequence
std::transform(in.begin(), in.end(), out.begin(), add_x(1)); 
assert(out[i] == in[i] + 1); // for all i

有关functor的几个好处。一个是不同于普通函数,它们可以包含状态。上面的例子创建一个函数,它添加42任何你给它。但是值42不是硬编码的,它被指定为构造函数参数,当我们创建我们的functor实例。我可以创建另一个加法器,它添加了27,只是通过调用具有不同值的构造函数。

There are a couple of nice things about functors. One is that unlike regular functions, they can contain state. The above example creates a function which adds 42 to whatever you give it. But that value 42 is not hardcoded, it was specified as a constructor argument when we created our functor instance. I could create another adder, which added 27, just by calling the constructor with a different value. This makes them nicely customizable.

如最后一行所示,你经常传递函数作为其他函数的参数,如std :: transform或其他标准库算法。你可以使用一个普通的函数指针,除了如上所述,函数可以定制,因为它们包含状态,使它们更灵活(如果我想使用一个函数指针,我必须写一个函数它的参数正好加1。函数是一般的,并且添加了你初始化它的任何东西),它们也可能更有效率。在上面的例子中,编译器确切地知道应该调用哪个函数 std :: transform 。它应该调用 add_x :: operator()。这意味着它可以内联该函数调用。这使它像我已经手动调用的向量的每个值上的函数一样高效。

As the last lines show, you often pass functors as arguments to other functions such as std::transform or the other standard library algorithms. You could do the same with a regular function pointer except, as I said above, functors can be "customized" because they contain state, making them more flexible (If I wanted to use a function pointer, I'd have to write a function which added exactly 1 to its argument. The functor is general, and adds whatever you initialized it with), and they are also potentially more efficient. In the above example, the compiler knows exactly which function std::transform should call. It should call add_x::operator(). That means it can inline that function call. And that makes it just as efficient as if I had manually called the function on each value of the vector.

如果我已经传递了一个函数指针,编译器couldn' t立即看到它指向的函数,所以除非它执行一些相当复杂的全局优化,否则它必须在运行时解引用指针,然后进行调用。

If I had passed a function pointer instead, the compiler couldn't immediately see which function it points to, so unless it performs some fairly complex global optimizations, it'd have to dereference the pointer at runtime, and then make the call.

这篇关于C ++ Functors - 及其用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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