C++ 在boost BGL中使用make_bfs_visitor代替具有bfs的派生visitor

C++ 在boost BGL中使用make_bfs_visitor代替具有bfs的派生visitor,c++,boost,graph,boost-graph,C++,Boost,Graph,Boost Graph,我(成功)为我的BFS实现了一个自定义访问者: (另见:) 在我的头文件中定义的访问者: class ListVisitor : public boost::default_bfs_visitor { public: template <typename Vertex, typename Graph> void discover_vertex(Vertex u, const Graph& /*g*/) const { std::cout

我(成功)为我的BFS实现了一个自定义访问者:

(另见:)

在我的头文件中定义的访问者:

class ListVisitor : public boost::default_bfs_visitor
{
public:
    template <typename Vertex, typename Graph>
    void discover_vertex(Vertex u, const Graph& /*g*/) const
    {
        std::cout << u << std::endl;
    }
};
我还尝试使用真实类型而不是模板类型:

void operator()(ListGraph_t::vertex_descriptor vert, const ListGraph_t& graph) const
但它只会改变错误:

Error   C2039   'discover_vertex': is not a member of ...  
Error   C2039   'examine_vertex': is not a member of ...  
Error   C2039   'examine_edge': is not a member of ...  
and so on..........  
我的问题:

  • 是否可以将make_bfs_visitor与广度优先访问算法一起使用? 我找到的所有示例都是通过广度优先搜索实现的
  • 这(一个或多个访问者,如examine_edge、examine_vertex)是否可以使用lambda表达式实现
  • 是否有任何理由(如性能惩罚)使用一种解决方案而不是另一种解决方案

  • 您需要使用
    typename
    来指示
    GRAPH
    中的
    vertex\u描述符
    是一种类型:

    template<typename GRAPH>
    void operator()(typename GRAPH::vertex_descriptor vert, const GRAPH& graph) const
                    ^^^^^^^^
    {
        std::cout << u << std::endl;
    }
    
    作为我们的产出

    boost::bfs_visitor<ListVisitor<boost::on_discover_vertex> >
    
    作为输出

    boost::bgl_named_params<boost::bfs_visitor<ListVisitor<boost::on_discover_vertex> >,
                  boost::graph_visitor_t, boost::no_property>
    

    在这两次更改之后,您的代码可以很好地编译。

    非常感谢!尤其是使用type_index.hpp来“调试”它是一个非常好的主意。您认为可以使用lambda表达式实现访问者吗?我认为不可能,访问者需要具有lambda表达式无法提供的事件过滤器。
    Error   C2039   'discover_vertex': is not a member of ...  
    Error   C2039   'examine_vertex': is not a member of ...  
    Error   C2039   'examine_edge': is not a member of ...  
    and so on..........  
    
    template<typename GRAPH>
    void operator()(typename GRAPH::vertex_descriptor vert, const GRAPH& graph) const
                    ^^^^^^^^
    {
        std::cout << u << std::endl;
    }
    
    auto v = boost::make_bfs_visitor(ListVisitor<boost::on_discover_vertex>());
    cout << boost::typeindex::type_id_with_cvr<decltype(v)>().pretty_name() << endl;
    
    boost::bfs_visitor<ListVisitor<boost::on_discover_vertex> >
    
    auto v2 = 
    boost::visitor(boost::make_bfs_visitor(ListVisitor<boost::on_discover_vertex>()));
           ^^^            ^^^
    cout << boost::typeindex::type_id_with_cvr<decltype(v2)>().pretty_name() << endl;
    
    boost::bgl_named_params<boost::bfs_visitor<ListVisitor<boost::on_discover_vertex> >,
                  boost::graph_visitor_t, boost::no_property>
    
    boost::breadth_first_visit(myGraph, 0, Q,
       boost::make_bfs_visitor(ListVisitor<boost::on_discover_vertex>()), colormap);