[C/C++标准库]_[初级]_[unary_function 和 binary_function]

unary_function

binary_function

一元和二元函数.

场景:

1.C++算法库里经常出现unary_function和binary_function类型,比如sort,count_if,find_if,或者std::map的构造函数等,这是用来对元素进行比较用的类型.

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. template <class Arg, class Result> struct unary_function;  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. template <class Arg1, class Arg2, class Result> struct binary_function;  

以下是从cplusplus.com摘录的说明:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 二元函数转换为一元函数 bind2nd  
  2. http://www.cplusplus.com/reference/functional/bind2nd/?kw=bind2nd  
  3. template <class Operation, class T>  
  4.   binder2nd<Operation> bind2nd (const Operation& op, const T& x);  
  5.   
  6. Return function object with second parameter bound  
  7. This function constructs an unary function object from the binary function object op by binding its second parameter to the fixed value x.  
  8.   
  9. To bind the first parameter to a specific value, see bind1st.  
  10.   
  11.   
  12. 一元函数 unary_function  
  13. http://www.cplusplus.com/reference/functional/unary_function/  
  14. 1.有返回值.  
  15. 2.只有一个参数.  
  16. In the case of unary function objects, this operator() member function takes a single parameter.  
  17.   
  18. unary_function is just a base class, from which specific unary function objects are derived. It has no operator() member defined (which derived classes are expected to define) - it simply has two public data members that are typedefs of the template parameters. It is defined as:   
  19.   
  20. template <class Arg, class Result>  
  21.   struct unary_function {  
  22.     typedef Arg argument_type;  
  23.     typedef Result result_type;  
  24.   };  
  25.   
  26. 二元函数 binary_function  
  27. http://www.cplusplus.com/reference/functional/binary_function/?kw=binary_function  
  28. template <class Arg1, class Arg2, class Result>  
  29.   struct binary_function {  
  30.     typedef Arg1 first_argument_type;  
  31.     typedef Arg2 second_argument_type;  
  32.     typedef Result result_type;  
  33.   };  

测试代码:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdint.h>  
  3. #include <time.h>  
  4. #include <algorithm>  
  5. #include <iostream>  
  6. #include <functional>  
  7. #include <vector>  
  8.   
  9. using namespace std;  
  10.   
  11. static void PrintVector(vector<int>& v)  
  12. {  
  13.     cout << "....PrintVector...." << endl;  
  14.     for (int i = 0; i < v.size(); ++i)  
  15.     {  
  16.         cout << v[i] << endl;  
  17.     }  
  18.   
  19. }  
  20.   
  21. int main(int argc, char const *argv[])  
  22. {  
  23.       
  24.     vector<int>  data;  
  25.   
  26.     srand( time(NULL) );  
  27.     cout << "RAND_MAX: " << RAND_MAX << endl;  
  28.     for (int i = 0; i < 10; ++i)  
  29.     {  
  30.         data.push_back(rand());  
  31.     }  
  32.     //1.std::less<int>  
  33.     sort(data.begin(),data.end(),std::less<int>());  
  34.     PrintVector(data);  
  35.   
  36.     //2.std::greator<int>  
  37.     sort(data.begin(),data.end(),std::greater<int>());  
  38.     PrintVector(data);  
  39.   
  40.     //3.std::greater_equal<int>  
  41.     //取>=0的整数  
  42.     int numbers[]={20,-30,10,10,-40,0};  
  43.     //bind2nd 把 binary_function 转换为 unary fucntion.  
  44.     int cx = std::count_if (numbers, numbers+6, std::bind2nd(std::greater_equal<int>(),0));  
  45.     std::cout << "There are " << cx << " non-negative elements.\n";  
  46.     vector<int> result;  
  47.     //1.找出所有大于0的整数,貌似没有好用的算法函数.  
  48.     for (int i = 0; i < 6; ++i)  
  49.     {  
  50.         if(numbers[i] > 0)  
  51.         {  
  52.             cout << numbers[i] << endl;  
  53.         }  
  54.     }  
  55.   
  56.     //4.小于等于10的整数.  
  57.     cx = std::count_if (numbers, numbers+6, std::bind2nd(std::less_equal<int>(),10));  
  58.     std::cout << "There are " << cx << " elements less or equal 10.\n";  
  59.   
  60.   
  61.   
  62.     return 0;  
  63. }  

输出:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. RAND_MAX: 32767  
  2. ....PrintVector....  
  3. 1315  
  4. 2075  
  5. 8984  
  6. 13167  
  7. 14730  
  8. 15153  
  9. 18586  
  10. 20698  
  11. 23082  
  12. 24151  
  13. ....PrintVector....  
  14. 24151  
  15. 23082  
  16. 20698  
  17. 18586  
  18. 15153  
  19. 14730  
  20. 13167  
  21. 8984  
  22. 2075  
  23. 1315  
  24. There are 4 non-negative elements.  
  25. 20  
  26. 10  
  27. 10  
  28. There are 5 elements less or equal 10  
  29. count_if :返回区间中满足指定条件的元素数目。

    template<class InputIterator, class Predicate>

       typename iterator_traits<InputIterator>::difference_type count_if(

          InputIterator _First,

          InputIterator _Last,

          Predicate _Pred

       );

    Parameters

    _First 输入迭代器,指向将被搜索的区间第一个元素的位置。

    _Last 输入迭代器,指向将被搜索的区间最后一个元素后面的。

    _Pred 用户自定义的 predicate function object ,定义了元素被计数需满足的条件。 predicate 只带一个参数,返回 true 或 false.

    Return Value

    满足断言(predicate)指定条件的元素数。

    Remarks

    这个模板函数是书法count的泛化版本,用断言指定的条件代替等于一个指定的值。

    Example

    1. // alg_count_if.cpp  
    2. // compile with: /EHsc  
    3. #include <vector>  
    4. #include <algorithm>  
    5. #include <iostream>  
    6.   
    7. bool greater10(int value)  
    8. {  
    9.     return value >10;  
    10. }  
    11.   
    12. int main()  
    13. {  
    14.     using namespace std;  
    15.     vector<int> v1;  
    16.     vector<int>::iterator Iter;  
    17.   
    18.     v1.push_back(10);  
    19.     v1.push_back(20);  
    20.     v1.push_back(10);  
    21.     v1.push_back(40);  
    22.     v1.push_back(10);  
    23.   
    24.     cout << "v1 = ( ";  
    25.     for (Iter = v1.begin(); Iter != v1.end(); Iter++)  
    26.        cout << *Iter << " ";  
    27.     cout << ")" << endl;  
    28.   
    29.     vector<int>::iterator::difference_type result1;  
    30.     result1 = count_if(v1.begin(), v1.end(), greater10);  
    31.     cout << "The number of elements in v1 greater than 10 is: "  
    32.          << result1 << "." << endl;  
    33. }  

    Output

     

    v1 = ( 10 20 10 40 10 )

    The number of elements in v1 greater than 10 is: 2.

    Requirements

    Header: <algorithm>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值