开发者

Using boost::depth_first_search with Visitor

开发者 https://www.devze.com 2023-02-04 19:47 出处:网络
As the title suggests, I\'m using boost::depth_first_search and using a Visitor (inheriting from boost::default_dfs_visitor) to implement some algorithm.

As the title suggests, I'm using boost::depth_first_search and using a Visitor (inheriting from boost::default_dfs_visitor) to implement some algorithm.

However, during the algorithm's run, I want to save some information in the visitor, to be queried later. However, the information is erased after the DFS is done, so I assume it uses a copy. Other than just u开发者_如何学Csing pointers for all private variables, is there a way to prevent this and make boost use my copy?


You could try passing your visitor wrapped in a boost::reference_wrapper.

Edit - teh codez

YourVisitorClass your_visitor;
boost::depth_first_search(your_graph, boost::ref(your_visitor), 
                          your_color_map);

boost::ref(your_visitor) returns a boost::reference_wrapper<YourVisitorClass>. When depth_first_search creates a copy of that arguments, it will copy the reference_wrapper instead of the visitor object. Copies of the reference will refer to the same instance as the original.


Does it really make sense that the information is part of the visitor?

My guess is that the information logically belongs with the graph, and should be stored there, not in the visitor.

You can store a reference to the graph in the visitor. Then, as the visitor traverses the graph, it can update the information that is stored with the graph.

The result is that it is OK to destroy the visitor when its work is done, because the results will persist as part of the graph.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号