c++ - 将 boost::depth_first_search 与访客一起使用

标签 c++ boost depth-first-search boost-graph

正如标题所示,我正在使用 boost::depth_first_search 并使用访问者(继承自 boost::default_dfs_visitor)来实现一些算法。

但是,在算法运行过程中,我想在访问者中保存一些信息,以供稍后查询。但是,DFS 完成后信息会被删除,所以我假设它使用了一个拷贝。除了对所有 私有(private)变量使用指针之外,是否有办法防止这种情况发生并让 boost 使用我的拷贝?

最佳答案

您可以尝试通过包裹在 boost::reference_wrapper 中的访问者.

编辑 - teh codez

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

boost::ref(your_visitor)返回 boost::reference_wrapper<YourVisitorClass> .当depth_first_search创建该参数的拷贝,它将复制 reference_wrapper 而不是访问者对象。引用的拷贝将引用与原始实例相同的实例。

关于c++ - 将 boost::depth_first_search 与访客一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4706841/

相关文章:

c++ - 如何使用 boost C++ 复制图形?

C++ 将 lexical_cast double boost 为字符串

boost - boost::bind 的实例是否在其生命周期内保留共享指针?

C++ 如何从线程生成函数捕获 Boost 中线程抛出的异常

java - 邻接矩阵 DFS 遍历以在有向图 (Java) 中查找从 x 到 y 的路径数

java - Java 中二叉树/图的深度优先搜索

c++ - Python初始化段错误

c++ - Netbeans下无DLL依赖Mingw

algorithm - 深度优先搜索会产生冗余吗?

c++ - 静态常量对象