|
Boost Users : |
Subject: Re: [Boost-users] is visitor object destroyed immediately after the execution of depth_first_search?
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2009-12-15 19:57:25
On Tue, 15 Dec 2009, Suresh Kumar wrote:
> Hi,
> my idea is to get the finishing order of vertices in dfs by pushing
> the vertices in a vector and return it later through a function return
> value from my visitor class object.
> //code snippet
> int main()
> {
> my_visitor vis;
> depth_first_search(g, visitor(vis));
> cout << "size of fin vector = " << vis.finishingOrder.size() << endl;
> //i get zero here!!!
> }
>
> class my_visitor: public default_dfs_visitor {
> public:
> my_visitor(){}
> void finish_vertex(Vertex u, const Graph &
> g){finishingOrder.push_back(u);};
> vector<Vertex> getFinishingOrder(void){return finishingOrder;}
> private:
> vector<Vertex> finishingOrder;
> };
>
> My problem: size of the finishingOrder vector is zero outside the
> my_visitor::finish_vertex method. I checked its size inside the
> my_visitor::finish_vertex method and its value is increasing every
> time a vertex is finished.
>
> Does I miss something in the understanding of the visitor concept?
Someone asked basically the same question in the thread "[Boost-users]
boost graph library, default_dfs_visitor, dfs". The visitor is copied by
the algorithm, so you will need to have the finishingOrder vector be a
reference to an externally declared vector. Have your main program
declare such a vector and have the visitor contain a reference member that
points to it; then the vector will be available even after the algorithm
finishes.
-- Jeremiah Willcock
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