代码之家  ›  专栏  ›  技术社区  ›  nabulke

帮助我使用这个boost::lambda::if_Then表达式调用函数

  •  2
  • nabulke  · 技术社区  · 15 年前

    请查看以下简化示例:

    #include <vector>
    #include <string>
    #include <algorithm>
    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    #include <boost/lambda/if.hpp>
    
    using namespace boost::lambda;
    namespace bl = boost::lambda;
    
    using namespace std;
    
    struct Item
    {
        string sachNr;
        int ist;
        int soll;
    };
    
    struct Printer
    {
        void operator()(const Item &item)
        {
            m_erg += item.sachNr;
        }
    
        operator string()
        {
            return m_erg;
        }
    
    private:
    
        string m_erg;
    };
    
    void TestFunction()
    {
        vector<Item> pickItems;
    
        string result = for_each(pickItems.begin(), pickItems.end(),   
            bl::if_then(bl::bind(&Item::ist, bl::_1) == bl::bind(&Item::soll, bl::_1), 
            Printer()));
    }
    

    错误消息(GCC)

    /TestCpp-build-desktop/../TestCpp/ctest.cpp:52: error: no matching function for call to ‘if_then(const boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::equal_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2, boost::lambda::function_action<2, boost::lambda::detail::unspecified> >, boost::tuples::tuple<int Item::* const, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2, boost::lambda::function_action<2, boost::lambda::detail::unspecified> >, boost::tuples::tuple<int Item::* const, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, Printer)’
    

    有什么提示吗?那代码怎么了?

    它应该为item.soll==item.ist的任何项调用打印机函数。

    谢谢你的帮助!

    3 回复  |  直到 15 年前
        1
  •  2
  •   visitor    15 年前

    bl::if_(condition)[function]

    for_each

    #include <vector>
    #include <string>
    #include <algorithm>
    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    #include <boost/lambda/if.hpp>
    
    using namespace boost::lambda;
    namespace bl = boost::lambda;
    
    using namespace std;
    
    struct Item
    {
        string sachNr;
        int ist;
        int soll;
    };
    
    struct Printer
    {
        typedef void result_type;
        void operator() (const Item &item, std::string& s) const
        {
            s += item.sachNr;
        }
    };
    
    void TestFunction()
    {
        vector<Item> pickItems;
    
        string result;
        for_each(pickItems.begin(), pickItems.end(), 
            bl::if_then(
                bl::bind(&Item::ist, bl::_1) == bl::bind(&Item::soll, bl::_1), 
                bl::bind(Printer(), bl::_1, boost::ref(result))
            )
        );
    }
    

    void TestFunction()
    {
        vector<Item> pickItems;
        string result;
        BOOST_FOREACH(const Item& item, pickItems) {
            if (item.ist == item.soll)
                result += item.sachNr;
        }
    
    }
    

        2
  •  1
  •   ronag    15 年前

     Printer printer;
     std::for_each(pickItems.begin(), pickItems.end(), bl::if_then(bl::bind(&Item::ist, bl::_1) == bl::bind(&Item::soll, bl::_1), bl::bind(&Printer::operator(), &printer, bl::_1)));
    

        3
  •  1
  •   icecrime    15 年前

    Printer

    std::string s;
    for_each(
        pickItems.begin(), pickItems.end(),
        bl::if_ (bl::bind(&Item::ist, bl::_1) == bl::bind(&Item::soll, bl::_1))
        [
            s += bl::bind(&Item::sachNr, bl::_1)
        ]
    );