如何在for_each中组合函数和谓词? [英] How to combine a function and a predicate in for_each?

查看:137
本文介绍了如何在for_each中组合函数和谓词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 for_each()部分容器中调用 Function $ c>?



我创建了一个 for_each_if()来执行

  for(i in shapes)
if(i.color == 1)
displayShape(i);

,呼叫看起来像

  for_each_if(shapes.begin(),shapes.end(),
bind2nd(ptr_fun(colorEquals),0),
ptr_fun(displayShape)

bool colorEquals(Shape& s,int color){
return s.color == color;
}

但是,我认为类似STL的算法不是我应该

p>

我没有想做

  for_each(shapes.begin(),shapes.end(),
bind2nd(ptr_fun(display_shape_if_color_equals),0));

因为在一个更复杂的情况下,函子名称会误导, / p>

  • *有一种方法可以访问结构的成员(如 colorEquals $ c> for_each 而不必创建一个函数? *



  • 解决方案

    如果您需要使用常规的for_each模仿if条件的函子。

      #include< algorithm> 
    #include< vector>
    #include< functional>
    #include< iostream>
    #include< boost / bind.hpp>

    using namespace std;

    struct incr {
    typedef void result_type;
    void operator()(int& i){++ i; }
    };

    struct is_odd {
    typedef bool return_type;
    bool operator()(const int& value){return(value%2)== 1; }
    };


    template< class Fun,class Cond>
    struct if_fun {
    typedef void result_type;
    void operator()(Fun fun,Cond cond,int& i){
    if(cond(i))fun(i);
    }
    };


    int main(){
    vector< int> vec;
    for(int i = 0; i <10; ++ i)vec.push_back(i);

    for_each(vec.begin(),vec.end(),boost :: bind(if_fun< incr,is_odd>(),incr(),is_odd(),_1)
    for(vector< int> :: const_iterator it = vec.begin(); it!= vec.end(); ++ it)
    cout< * it< ;
    }

    不幸的是,我的模板hackery不够好用bind1st和bind2nd因为它不知何故让粘合剂返回是一个 unary_function 混淆,但它看起来不错, boost :: bind 。我的例子不是完美的,因为它不允许Func传递到if_fun返回,我想有人可以指出更多的缺陷。欢迎建议。


    How can you call a Function over some part of a container, using for_each() ?

    I have created a for_each_if() to do a

    for( i in shapes )
        if( i.color == 1 )
            displayShape(i);
    

    and the call looks like

    for_each_if( shapes.begin(), shapes.end(),
                           bind2nd( ptr_fun(colorEquals), 0 ),
                           ptr_fun( displayShape ) );
    
    bool colorEquals( Shape& s, int color ) {
        return s.color == color;
    }
    

    However, I feel immitating STL-like algorithms is not something that I should be doing.

    1. Is there a way to use only existing STL keywords to produce this ?

      I did not want to do a

       for_each( shapes.begin(), shapes.end(),
                         bind2nd( ptr_fun(display_shape_if_color_equals), 0 ) );
      

      because, in a more complicated case, the functor name would be misleading with respect to what the functor

    2. *Is there a way to access a struct's member (like colorEquals) for functions like for_each without having to create a function ? *

    解决方案

    To use a regular for_each with an if you would need a Functor that emulates an if condition.

    #include <algorithm>
    #include <vector>
    #include <functional>
    #include <iostream>
    #include <boost/bind.hpp>
    
    using namespace std;
    
    struct incr {
      typedef void result_type;
      void operator()(int& i) { ++i; }
    };
    
    struct is_odd {
      typedef bool return_type;
      bool operator() (const int& value) {return (value%2)==1; }
    };
    
    
    template<class Fun, class Cond>
    struct if_fun {
      typedef void result_type;
      void operator()(Fun fun, Cond cond, int& i) {
        if(cond(i)) fun(i);
      }
    };
    
    
    int main() {
      vector<int> vec;
      for(int i = 0; i < 10; ++i) vec.push_back(i);
    
      for_each(vec.begin(), vec.end(), boost::bind(if_fun<incr, is_odd>(), incr(), is_odd(), _1));
      for(vector<int>::const_iterator it = vec.begin(); it != vec.end(); ++it)
        cout << *it << " ";
    }
    

    Unfortunately my template hackery isn't good enough to manage this with bind1st and bind2nd as it somehow gets confusing with the binder being returned being a unary_function but it looks pretty good with boost::bind anyhow. My example is no means perfect as it doesn't allow the Func passed into if_fun to return and I guess somebody could point out more flaws. Suggestions are welcome.

    这篇关于如何在for_each中组合函数和谓词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆