|
Boost Users : |
Subject: Re: [Boost-users] Boost.lambda: how to sort this vector with function?
From: Roland Bock (rbock_at_[hidden])
Date: 2009-02-09 01:54:51
Joost Kraaijeveld wrote:
> I want to sort a vector of Edges according to their remaining estimated
> distance to the finish point (an own implementation of a graph
> algorithm).
>
> How do I sort this vector with edges:
>
> // Returns the cost between start and finish, given a graph
> int CostEstimate( const Graph& aGraph, const Vertex& start, const Vertex& finish)
> ...
>
> // Member function of a graph
> std::vector< Edge > v = ...;
> // Sort them according to the cost
> std::sort( v.begin(),
> v.end(),
> boost::lambda::bind( CostEstimate( (*this),
> boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_1),
> finish))
> <
> boost::lambda::bind( CostEstimate( (*this),
> boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_2),
> finish)));
>
> where otherSide is a const function of the class Edge which returns a
> "const Vertex&"
>
> Compiling this results in an error:
>
> error: invalid initialization of reference of type âconst Vertex&â from
> expression of type âconst
> boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<3, boost::lambda::function_action<3, boost::lambda::detail::unspecified> >, boost::tuples::tuple<const Vertex& (Edge::* const)(const Vertex&)const, const Vertex* 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> > >â
>
> TIA
>
Hi,
I am sure there is some way to extend the code you wrote in such a way
that it compiles and works as it should. My question would be: Are you
going to still understand it in, say, half a year? Four binds in one
expression is certainly not unheard of, but personally I would do it
with one bind and an old-fashioned function, see attached code.
Hope I got your settings right. I was a bit confused by otherSide being
a function of edge. This would mean that start is an Edge, while finish
is a Vertex in the sort-code. But start and finish are both Vertexes in
the CostEstimate function...
To avoid such wondering, it would help if you could provide a small but
complete program which shows the problem. Nothing more and nothing less.
Regards,
Roland
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net