简体   繁体   中英

Implement C++11 lambda using boost

I have a problem trying to convert my C++11 code snippet to boost.

Let's suppose I have a simple class:

class C
{

public:

    typedef std::vector<std::string> Info;

public:

    explicit C(Info const& info)
        : info_(info)
    {}

    std::string text(Info::size_type i) const
    {
        return info_[i];
    }

private:

    Info info_;

};

Now if I have a vector of C classes I would like to get the maximum width of a specific C::Info element using std::max_element algorithm.

With C++11 lambdas I could do it the following way:

C::Info::size_type const Info_0 = 0;
C::Info::size_type const Info_1 = 1;
C::Info::size_type const Info_2 = 2;

int get_max_width(std::vector<C> const& c_vec, C::Info::size_type info_n)
{
    std::vector<C>::const_iterator max = std::max_element(c_vec.cbegin(), c_vec.cend(),
        [info_n](C const& lhs, C const& rhs)
        { return lhs.text(info_n).length() < rhs.text(info_n).length(); });

    return max != c_vec.end() ? max->text(info_n).length() : 0;
}

But the problem is that I can't use C++11, so I have to go with boost.

At the moment, the code I was able to come up with looks as follows:

int get_max_width(std::vector<C> const& c_vec, C::Info::size_type info_n)
{
    std::vector<C>::const_iterator max = std::max_element(c_vec.cbegin(), c_vec.cend(),
        boost::lambda::bind(&C::text, boost::lambda::_1, boost::lambda::var(info_n)) <
        boost::lambda::bind(&C::text, boost::lambda::_2, boost::lambda::var(info_n)));

    return max != c_vec.end() ? max->text(info_n).length() : 0;
}

Though it's clearly not what I want. The comparison is performed on the std::string objects, and I'm stuck trying to figure out how to call that length() function...

Your help is quite appreciated.

Here's a C++03 solution that uses Boost.Phoenix

#include <functional>
#include <boost/phoenix.hpp>

int get_max_width(std::vector<C> const& c_vec, C::Info::size_type info_n)
{
    namespace phx = boost::phoenix;
    using namespace phx::arg_names;

    std::vector<C>::const_iterator max = std::max_element(c_vec.begin(), c_vec.end(),
        phx::bind(std::mem_fun_ref(&std::string::length), phx::bind(&C::text, arg1, phx::val(info_n))) < 
        phx::bind(std::mem_fun_ref(&std::string::length), phx::bind(&C::text, arg2, phx::val(info_n)))
      );

    return max != c_vec.end() ? max->text(info_n).length() : 0;
}

I haven't done too much testing with it, but it seems to work here .

Not necessarily the answer you are asking for, but for a non c++11 approach it is useful to know that lambda expressions are similar to structs with a function operator.

So your example could be implemented something like this:

struct lambda_max
{
    const C::Info::size_type& info_n;

    lambda_max(const C::Info::size_type& info_n):info_n(info_n) {}

    bool operator()(C const& lhs, C const& rhs) const
    {
        return lhs.text(info_n).length() < rhs.text(info_n).length();
    }
};

int get_max_width(std::vector<C> const& c_vec, C::Info::size_type info_n)
{
    std::vector<C>::const_iterator max = std::max_element(c_vec.cbegin(), c_vec.cend(),
        lambda_max(info_n));

    return max != c_vec.end() ? max->text(info_n).length() : 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM