STL预定义函数对象

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

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

[cpp]  view plain  copy
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7.   
  8. using namespace std;  
  9.   
  10. // plus,预定义好的函数对象,能实现不同类型数据的 + 运算  
  11. // 实现了数据类型和算法的分离,通过函数对象技术实现的  
  12. void play01()  
  13. {  
  14.     /* plus函数对象原型 
  15.     template 
  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 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 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 
  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()); // 从大到小排序  
  64.     for (vector::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(), 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
plus stringAdd;
sres = stringAdd(sva1,sva2);
减法:minus
乘法:multiplies
除法divides
求余:modulus
取反:negate
negate intNegate;
ires = intNegate(ires);
Ires= UnaryFunc(negate(),Ival1);

3)关系函数对象 
等于equal_to
equal_to stringEqual;
sres = stringEqual(sval1,sval2);
不等于not_equal_to
大于 greater
大于等于greater_equal
小于 less
小于等于less_equal

4)逻辑函数对象 
逻辑与 logical_and
logical_and indAnd;
ires = intAnd(ival1,ival2);
dres=BinaryFunc( logical_and(),dval1,dval2);
逻辑或logical_or
逻辑非logical_not
logical_not IntNot;
Ires = IntNot(ival1);
Dres=UnaryFunc( logical_not,dval1);

你可能感兴趣的:(STL学习)