《C++标准库》

函数对象

使用bind时注意,占位符有自己的命名空间std::placeholders,如果不在程序开始处using std::placeholders,那么就要写成:

std::bind(std::logical_and<bool>(),

              std::bind(std::greater_equal<int>(),std::placeholders::_1,50),

              std::bind(std::less_equal<int>(),std::placeholders::_1,80));

上面的表现出单参判别式,等价于x>=50&&x<=80。

 bind调用全局函数:bind(myToupper,_1),其中myToupper:

char myToupper(char c)
{
      ...
}

 bind调用成员函数:bind(&Person::print,_1,"This is: ");相当于调用param1.print("This is: ");

函数对象是一个类,因此可作为template模板实参,而寻常函数无法这样。

函数对象拥有内部状态(类的私有成员)。

for_each()算法可以返回其参数中的函数对象:

MeanValue mv=for_each(coll.begin(),coll.end(),MeanValue());

 

原文地址:https://www.cnblogs.com/ph829/p/5664447.html