C++STL 函数对象和谓词

函数对象:重载函数调用操作符的类,其对象常称为函数对象。

函数对象属于类对象,能突破函数概念,保持类的状态

谓词:

一元函数对象:函数参数1个;

二元函数对象:函数参数2个;

一元谓词 函数参数1个,函数返回值是bool类型,可以作为一个判断式

谓词可以使一个仿函数,也可以是一个回调函数。

二元谓词 函数参数2个,函数返回值是bool类型

一元谓词函数举例如下

1,判断给出的string对象的长度是否小于6

bool GT6(const string &s)

{

return s.size() >= 6;

}

2,判断给出的int是否在38之间

bool Compare( int i )

{

return ( i >= 3 && i <= 8 );

}

二元谓词举例如下

1,比较两个string对象,返回一个bool值,指出第一个string是否比第二个短

bool isShorter(const string &s1, const string &s2)

{

return s1.size() < s2.size();

}

猜你喜欢

转载自www.cnblogs.com/smh2015/p/9656414.html