|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1、预定义函数对象类
标准模板库STL提前定义了很多预定义函数对象类,需包含#include <functional> 。
实例: plus<Typenmae> 预定义好的函数对象类 能实现不同数据类型的 + 运算 及equal_to<Type>
- plus<int> intAdd;
- int x = 10;
- int y = 20;
- int z = intAdd(x, y);
- cout << "z: " << z << endl;
- plus<string> stringAdd;
- string s1 = "abc";
- string s2 = "123";
- string s3 = stringAdd(s1, s2);
- cout << "s3:" << s3 << endl;
- vector<string> v1;
- v1.push_back("bbb");
- v1.push_back("aaa");
- v1.push_back("ccc");
- v1.push_back("bbb");
- v1.push_back("bbb");
-
- sort(v1.begin(), v1.end(), greater<string>()); // 预定函数对象类的匿名对象
- // 求“bbb”出现的次数
- // equal_to<string>()匿名函数对象 有两个参数 left参数来自容器 right参数来自sb
- // bind2nd => 函数适配器:把预定义函数参数和第二个参数进行绑定
- string sb = "bbb";
- int num = count_if(v1.begin(), v1.end(), bind2nd(equal_to<string>(), sb));
- cout << "num:" << num << endl;
复制代码
1. 预定义算术函数对象
预定义的函数对象支持加、减、乘、除、求余和取反。调用的操作符是与type相关联的实例
加法:plus<Types>
plus<string> stringAdd;
sres = stringAdd(sva1,sva2);
减法:minus<Types>
乘法:multiplies<Types>
除法divides<Tpye>
求余:modulus<Tpye>
取反:negate<Type>
negate<int> intNegate;
ires = intNegate(ires);
Ires= UnaryFunc(negate<int>(),Ival1);
2. 关系函数对象
等于equal_to<Tpye>
equal_to<string> stringEqual;
sres = stringEqual(sval1,sval2);
不等于not_equal_to<Type>
大于 greater<Type>
大于等于greater_equal<Type>
小于 less<Type>
小于等于less_equal<Type>
3. 逻辑函数对象
逻辑与 logical_and<Type>
logical_and<int> indAnd;
ires = intAnd(ival1,ival2);
dres=BinaryFunc( logical_and<double>(),dval1,dval2);
逻辑或logical_or<Type>
逻辑非logical_not<Type>
logical_not<int> IntNot;
Ires = IntNot(ival1);
Dres=UnaryFunc( logical_not<double>,dval1);
2、函数适配器
STL中已经定义了大量的函数对象,但有时候需要对函数的返回值进行进一步的简单计算,或者填上多余的参数,不能直接带入算法。函数适配器实现了这一功能,将函数对象转换为另一种符合要求的函数对象。
函数适配器可以分为四类:
① 绑定适配器(bind adaptor)
② 组合适配器(composite adaptor)
③ 指针函数适配器(pointer function adaptor)
④ 成员函数适配器(member function adaptor )
STL所有的函数适配器如下表:
直接构造STL中的函数适配器通常会导致冗长的类型声明,为了简化函数适配器的构造,STL还提供了函数适配器的辅助函数,借助于泛型自动推断技术,无需显示的类型声明便可以实现函数适配器的构造,辅助函数如下表:
常用函数函数适配器:
1. 绑定器(binder): binder通过把二元函数对象的一个实参绑定到一个特殊的值上,将其转换成一元函数对象。C++标准库提供两种预定义的binder适配器:bind1st和bind2nd,前者把值绑定到二元函数对象的第一个实参上,后者绑定在第二个实参上。
2. 取反器(negator) : negator是一个将函数对象的值翻转的函数适配器。标准库提供两个预定义的ngeator适配器:not1翻转一元预定义函数对象的真值,而not2翻转二元谓词函数的真值。
即:
bind1st(op, value)
bind2nd(op, value)
not1(op)
not2(op)
应用举例:
- struct IsGreat
- {
- public:
- IsGreat(int i)
- {
- m_num = i;
- }
- bool operator()(int &num)
- {
- if (num > m_num) return true;
- return false;
- }
- private:
- int m_num;
- };
- void main0011()
- {
- vector<int> v1;
- for (int i = 0; i < 10; i++)
- {
- v1.push_back(i + 1);
- }
- for (auto each : v1)
- {
- cout << each << " ";
- }
- cout << endl;
- int num = count(v1.begin(), v1.end(), 3);
- cout << "num:" << num << endl;
- // 通过 谓词 求大于2的个数:
- int num2 = count_if(v1.begin(), v1.end(), IsGreat(2));
- cout << "num2:" << num2 << endl;
- // 通过 预定义的函数对象 求大于2的个数:
- // greater<int>() 有两个参数 左参数来自容器的元素 右参数固定成2(通过函数适配器bin2nd做的)
- int num3 = count_if(v1.begin(), v1.end(), bind2nd(greater<int>(), 2));
- cout << "num2:" << num3 << endl;
- // 求 奇数的个数
- int num4 = count_if(v1.begin(), v1.end(), bind2nd(modulus<int>(), 2));
- cout << "奇数的个数 num4:" << num4 << endl;
- // 求 偶数的个数 取反器
- int num5 = count_if(v1.begin(), v1.end(), not1( bind2nd(modulus<int>(), 2)));
- cout << "偶数的个数 num5:" << num5 << endl;
- }
复制代码
|
|