仿函数(functors)其实就是重载了operator()的对象。
下面简单先看看它的一个例子:
1 #include <iostream> 2 using namespace std; 3 4 template<typename T> 5 struct m_plus 6 { 7 T operator()(const T& x, const T& y) { return x + y; } 8 }; 9 10 int main(int argc, char *argv[]) 11 { 12 // 定义其对象 调用其operator() 13 m_plus<int> op; 14 cout << op(1, 2) << endl; 15 // 产生一个匿名对象 这是仿函数的主流用法 16 cout << m_plus<int>()(1, 2) << endl; 17 return 0; 18 }