在上一篇文章中介绍了C++11新引入的lambda表达式(C++支持闭包的实现),现在我们看一下lambda的出现对于我们编程习惯的影响,毕竟,C++11历经10年磨砺,出140新feature,对于我们的programming idiom有深远影响。我们先看一下是不是lambda的出现对于仿函数的影响。
1) 仿函数wikipedia 的定义:
A function object, also called a functor, functional, or functionoid, is a computer programming construct allowing an object to be invoked or called like it was an ordinary function, usually with the same syntax. |
class PrintInt
{
public:
void operator()(int elem) const
{
std::cout<<elem<<' ';
}
};
std::vector<int> v;
for_each(v.begin(),v.end(), PrintInt());
//C++ 11 lambda stype
for_each(begin(v), en