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.
get_max_width(). Not a huge deal, but more of an annoying restriction.