您的位置:首页 > 编程语言 > C语言/C++

《C++标准库》

2016-07-12 18:09 239 查看
函数对象

使用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());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: