STL预定义函数对象

预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象

1)使用预定义函数对象:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <algorithm>  
  4. #include <string>  
  5. #include <vector>  
  6. #include <functional>  
  7.   
  8. using namespace std;  
  9.   
  10. // plus,预定义好的函数对象,能实现不同类型数据的 + 运算  
  11. // 实现了数据类型和算法的分离,通过函数对象技术实现的  
  12. void play01()  
  13. {  
  14.     /* plus函数对象原型 
  15.     template<class _Ty = void> 
  16.     struct plus 
  17.         : public binary_function < _Ty, _Ty, _Ty > 
  18.     {   // functor for operator+ 
  19.         _Ty operator()(const _Ty& _Left, const _Ty& _Right) const 
  20.         {   // apply operator+ to operands 
  21.             return (_Left + _Right); 
  22.         } 
  23.     }; 
  24.     */  
  25.   
  26.     plus<int> intAdd; // 预定义函数对象  
  27.     int x = 10;  
  28.     int y = 20;  
  29.     int z = intAdd(x, y); // x + y;  
  30.     cout << "z: " << z << endl;  
  31.     // z : 30  
  32.   
  33.     plus<string> stringAdd;  
  34.     string s1 = "lucifer";  
  35.     string s2 = "zhang";  
  36.     string s3 = stringAdd(s1, s2);  
  37.     cout << "s3: " << s3 << endl;  
  38.     // s3: luciferzhang  
  39. }  
  40.   
  41. void play02()  
  42. {  
  43.     vector<string> v;  
  44.     v.push_back("lucifer");  
  45.     v.push_back("zhang");  
  46.     v.push_back("yao");  
  47.     v.push_back("qi");  
  48.   
  49.     /* 
  50.     template<class _Ty = void> 
  51.     struct greater 
  52.         : public binary_function < _Ty, _Ty, bool > 
  53.     {   // functor for operator> 
  54.         bool operator()(const _Ty& _Left, const _Ty& _Right) const 
  55.         {   // apply operator> to operands 
  56.             return (_Left > _Right); 
  57.         } 
  58.     }; 
  59.     */  
  60.     //缺省情况下,sort()用底层元素类型的小于操作符以升序排列容器的元素。  
  61.     //为了降序,可以传递预定义的类模板greater,它调用底层元素类型的大于操作符:  
  62.   
  63.     sort(v.begin(), v.end(), greater<string>()); // 从大到小排序  
  64.     for (vector<string>::iterator it = v.begin(); it != v.end(); ++it) {  
  65.         cout << *it << ' ';  
  66.     }  
  67.     cout << endl;  
  68.     // zhang yao qi lucifer  
  69.   
  70.     string sl = "lucifer";  
  71.     int num = count_if(v.begin(), v.end(), bind2nd(equal_to<string>(), sl));  
  72.     cout << "count of 'lucifer': " << num << endl;  
  73.     // count of 'lucifer': 1  
  74. }  
  75.   
  76. int main()  
  77. {  
  78.     play01();  
  79.     play02();  
  80.   
  81.     return 0;  
  82. }  
2)算术函数对象  预定义的函数对象支持加、减、乘、除、求余和取反。调用的操作符是与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);

3)关系函数对象 
等于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>

4)逻辑函数对象 
逻辑与 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);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值