函子 - 它们相对于未实现运算符的其他类具有什么能力 ( )
Functor - what power do they have over other classes that didn't implement operator ( )
我试图理解函子的"力量"。
好的,所以它们是指向函数的指针,但是它们能做什么,而其他没有实现operator()
的类不能做什么?
例如:
#include <iostream>
#include <assert.h>
#include <vector>
using namespace std;
class MultiplyBy
{
private:
int factor;
public:
MultiplyBy(int x) : factor(x) {}
int operator () (int other) const
{
return factor * other;
}
};
int main()
{
MultiplyBy temp(5);
int myResult = temp(5); // now myResult holds 25
cout << myResult << endl;
return 0;
}
我们带走他的另一个朋友
class MultiplyOther
{
private:
int factor;
public:
MultiplyOther(int x) : factor(x) {}
int invokeMultiplyMe(int _other)
{
return _other * factor;
}
};
并做同样的事情:
int main()
{
// MultiplyOther
MultiplyOther notFunctor(4);
int myOther = notFunctor.invokeMultiplyMe(3);
cout << myOther << endl;
return 0;
}
我们得到:
25
12
那么,函子的真正力量是什么?是两个类都保存了状态,还是我在这里遗漏了什么?
首先,它们有一个统一的签名,例如,只需要知道它们必须调用某些东西的算法就可以使用。
在您的示例中,任何期望调用接受int
并返回一个(或可从/convertible 构造到 int
的类型(的代码都可以使用 MultiplyBy
的实例。使用第二个示例需要代码了解invokeMultiplyByMe
方法。
请注意,函子根本不是指向函数的指针,如问题中所述。它们是类,其实例可以使用函数调用的语法进行调用。
函子对类没有任何特殊属性,但它们可以用作具有更高功能的函数。函数通常不能有状态,如果它有(通过使用静态变量(,则状态在所有函数调用之间共享。
函子具有类的所有属性(尤其是状态(,但可以用作函数。一个很好的例子是用于 STL 算法的函子:
class Add
{
public:
Add(){sum = 0;};
void operator()(int i)
{
sum += i;
}
int getSum(){return sum;};
private:
int sum;
}
int main()
{
std::vector<int> vint;
vint.push_back(2);
vint.push_back(3);
vint.push_back(4);
}
Add adder;
adder = std::for_each( vint.begin(); vint.end(), adder);
cout << adder.getSum();
"所以它们是指向函数的指针" - 函数指针和函子完全不同。前者只是指向原始函数的普通指针。它不持有任何状态。但是,函子或函数对象保存状态。函子是定义运算符((的任何类。因此,该类的任何对象都可以以函数的方式调用。
来自C++入门的代码片段...
template<class T>
class TooBig
{
private:
T cutoff;
public:
TooBig(const T & t) : cutoff(t) {}
bool operator()(const T & v) { return v > cutoff; }
};
使用此类,我们可以创建具有不同截止值的多个对象。太大截止1(10(;太大的截断2(20(;
使用函子的最佳位置之一是模板。例如,采用列表模板。它有一个remove_if函数,该函数删除列表中从参数返回 true 的任何值。
list list1;//假设它有一些值列表列表2;假设它有一些值
现在,调用 list1.remove_if(cutoff1(;//应用 list1 的所有 elment,并删除那些大于 cutOff1 中存储的值的 elment。列表 2 类似的事情。
感谢!
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 函子 - 它们相对于未实现运算符的其他类具有什么能力 ( )
- 什么是编程语言为派生类重新定义方法的能力