C++ STL(29):Function Object Adapter(函数对象适配器)

29 篇文章 0 订阅
29 篇文章 1 订阅
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
#include <functional>
#include <cstring>
 
//Function Object Adapter
int main()
{
     /************************************************************************/
     //binder1st:绑定第一个参数
     /*
      template<class Operation, class Type>
      binder1st <Operation> bind1st(
           const Operation& _Func,
           const Type& _Left
       );
     */
     /************************************************************************/
     std::list<int> L;
     std::generate_n(std::front_inserter(L), 10, []{return rand() % 10; });
     std::copy(L.begin(), L.end(), std::ostream_iterator<int>(std::cout, " "));
     std::cout << std::endl;

     using Iterator = std::list<int>::iterator;
     Iterator it = std::find_if(L.begin(), L.end(), std::bind1st(std::equal_to<int>(), 3));
     if (it == L.end())
          std::cout << "Not found! " << std::endl;
     else
          std::cout << "Found the first 3 in index: " << std::distance(it, L.begin()) << std::endl;
    
     /************************************************************************/
     //binder2nd:绑定第二个参数
     /*
      template<class Operation, class Type>
      binder2nd <Operation> bind2nd(
           const Operation& _Func,
           const Type& _Right
       );
     */
     /************************************************************************/
     it = std::find_if(L.begin(), L.end(), std::bind2nd(std::less_equal<int>(), 3));
     if (it == L.end())
          std::cout << "Not found! " << std::endl;
     else
          std::cout << "Found the less than 3 in index: " << std::distance(L.begin(), it) << std::endl;
     /************************************************************************/
     //pointer_to_unary_function和pointer_to_binary_function不直接使用,应该使用辅助函数ptr_fun来代替
     /*
      template<class Arg, class Result>
      class pointer_to_unary_function
           : public unary_function<Arg, Result>
      {
      public:
           explicit pointer_to_unary_function(
                Result(*_pfunc)(Arg)
            );
           Result operator()(
                Arg _Left
            ) const;
      };
     */
     /*
      template<class Arg1, class Arg2, class Result>
      class pointer_to_binary_function
           : public binary_function <Arg1, Arg2, Result>
      {
      public:
           explicit pointer_to_binary_function(
                Result(*_pfunc)(Arg1, Arg2)
           );
            operator()(
                Arg1 _Left,
                Arg2 _Right
           ) const;
      };
     */
     /*
      template<class Arg, class Result>
      pointer_to_unary_function<Arg, Result, Result(*)(Arg)>
           ptr_fun(Result(*_pfunc)(Arg));
      template<class Arg1, class Arg2, class Result>
      pointer_to_binary_function<Arg1, Arg2, Result, Result(*)(Arg1, Arg2)>
           ptr_fun(Result(*_pfunc)(Arg1, Arg2));
     */
     /************************************************************************/
     std::list<char*> lc;
     lc.push_back("apple");
     lc.push_back("pear");
     lc.push_back("orange");
     lc.push_back("banana");
 
     using It_char = std::list<char*>::iterator;
     It_char ic = std::find_if(lc.begin(), lc.end(), std::not1(std::bind2nd(std::ptr_fun(strcmp), "orange")));
     if (ic != lc.end())
          std::cout << "The next of orange is " << *++ic << std::endl;
 
     /************************************************************************/
     //unary_negate和binary_negate不直接使用,使用辅助函数not1和not2来代替
     /*
      template<class Predicate>
      class unary_negate
           : public unary_function<
           typename Predicate::argument_type,
           bool>
      {
      public:
           explicit unary_negate(
                const Predicate& _Func
            );
           bool operator()(
                const typename Predicate::argument_type& _Left) const;
      };
     */
     /*
      template<class Operation>
      class binary_negate
           : public binary_function <
           typename Operation::first_argument_type,
           typename Operation::second_argument_type,
           bool>
      {
      public:
           explicit binary_negate(
                const Operation& _Func
            );   
           bool operator()(
                const typename Operation::first_argument_type& _Left,
                const typename Operation::second_argument_type& _Right
            ) const;
      };
     */
     /************************************************************************/
     //略
     return 0;
}


====================打个广告,欢迎关注====================

QQ:
412425870
微信公众号:Cay课堂

csdn博客:
http://blog.csdn.net/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)




点击群号或者扫描二维码即可加入QQ群:

180479701(2群)




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值