label_graph上的breadth_first_search

时间:2016-03-09 00:48:43

标签: c++ boost-graph

如果使用labeled_graph调用breadth_first_search,假定以下设置? - 导致2个错误:

二进制' [' :找不到哪个运算符采用了类型' Vertex'的右手操作数。 (或者没有可接受的转换)

错误2' .id'必须有class / struct / union

#include<iostream>
#include<boost/graph/adjacency_list.hpp>
#include<boost/graph/breadth_first_search.hpp>
#include <boost/graph/labeled_graph.hpp>

using namespace boost;

struct NodeInfo{int id;};
struct EdgeInfo{};

typedef boost::labeled_graph< boost::adjacency_list<
    boost::vecS, boost::vecS, boost::undirectedS, NodeInfo, EdgeInfo>,
    std::string> Graph;

typedef boost::graph_traits<Graph>::vertex_descriptor GridVertex;

class Topology 
{
public:

    Graph grid;
    std::map<std::string, GridVertex> vertices; //_id to Edge

    struct custom_visitor : public boost::default_bfs_visitor
    {
        Graph& grid;

        custom_visitor(Graph& grid) :grid(grid)  {}

        template <typename Vertex, typename Graph>
        void discover_vertex(Vertex v, const Graph& g)
        {

            //vertex(...) in breadth_first_search is causing: 
            //binary '[' : no operator found which takes a right-hand operand of 
            //type 'Vertex' (or there is no acceptable conversion)  
            //left of .id must have class...
            int m = grid[v].id;

        }
    };

    void GetShortestPath(std::string s_id, std::string t_id)
    {
        custom_visitor vis(grid);

        //vertex(...) causes error
        breadth_first_search(grid.graph(), vertex(vertices[s_id],grid.graph()), visitor(vis));
    }

    void BuildNet()
    {
        Graph g;
        GridVertex v;

        v = add_vertex("A", NodeInfo(), g);
        vertices["A"] = v;

        v = add_vertex("B", NodeInfo(), g);
        vertices["B"] = v;

        add_edge_by_label("A", "B", EdgeInfo(), g);
    }
};


int main()
{
    Topology net;
    net.GetShortestPath("A", "B");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

为什么使用labeled_graph?

标记图与 adjacency_list具有不同的界面。这并不奇怪,因为,否则会有什么意义:)

因此,如果vertex(...) causes error使用grid.vertex(s_id)

breadth_first_search(grid.graph(), grid.vertex(s_id), visitor(vis));

在访问者中,使用实际图表,以便您可以使用其operator[]

int m = grid.graph()[v].id;

或者,实际上为什么不使用为此目的而存在的第二个参数:

void discover_vertex(Vertex v, const Graph& g) {
    int m = g[v].id;
}

努力从代码中提出明智的例子: Live On Coliru