|
Boost Users : |
Subject: Re: [Boost-users] [BGL] Unable to sort list of edge_descriptors
From: Cedric Laczny (cedric.laczny_at_[hidden])
Date: 2010-11-07 10:41:18
Hi Pete,
On Sunday, 7. November 2010 07:20:50 Peter Wright wrote:
>
> One way to fix it would be as Cedric suggested, and that's the model I
> used in my douglass-working.cpp (attached). I changed SortByName so it
> requires a const Graph& in its constructor, stores a reference to that
> in a private variable, *then* changed SortByName::operator() to take
> two edgeDescriptor args and return bool.
>
> It is dicey in one way, in that it stores a *reference* to its Graph -
> so if the SortByName object outlives the Graph then it could blow up
> spectacularly. But the alternative of copying the entire graph seemed
> a bit excessive.
>
>
> Another way to fix it (and also avoid the reference-storing problem)
> requires using Boost::Bind - change SortByName to this:
>
> ----------------------------------------------------------------------
> template <typename G, typename ED>
> struct SortByName
> {
> typedef bool result_type; // required for use by STL algorithms.
> result_type operator()(G& g, const ED& a, const ED& b)
> {
> return g[a].eName < g[b].eName;
> }
> };
> ----------------------------------------------------------------------
>
> ...and the A.sort call to this:
>
> A.sort( boost::bind(SortByName<Graph, edgeDescriptor>(), g, _1, _2) );
>
> That seems to work fine, and may be a closer match to what you
> originally intended.
>
I have to agree that your solution is nicer with respect to the living time of
the graph and struct, respectively.
Those are things that are easily neglected and can hit you very bad.
Therefore, note to myself: keep such things always in the back of your head!
>
> Pete.
Best regards and thank you for the idea,
Cedric
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