gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:29:31 25 4
gpt4 key购买 nike

正如标题所示,我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com