首页 文章

BGL中边的自定义属性

提问于
浏览
5

我开始使用BGL进行一些与图形相关的任务 . 我有很多边缘,每条边都有几个属性,其中一个是它的重量 . (所有属性都是浮点数和整数) . 由于我之前从未使用过BGL(和/或类似的CPP库),所以我对所有这些类型,类以及如何正确使用它有点迷茫 .

我像这样添加边缘:

struct EdgeProperty
{
    int weight;
    float e1;
    float e2;
};

typedef adjacency_list<vecS, vecS, bidirectionalS, no_property, EdgeProperty> Graph;
...
EdgeProperty prop;
node1 = ...;
node2 = ...;
prop.e1 = ...;
prop.e2 = ...;
prop.weight = ...;

add_edge(node1, node2, prop, g);

然后,我需要稍后访问我的属性,我想这样做:

property_map<Graph, EdgeProperty>::type EdgeWeightMap = get(EdgeProperty, g);
w = get(EdgeWeightMap,some_edge);

但是,这甚至都没有编译 . 它在错误消息中说:

error: no type named ‘kind’ in ‘struct EdgeProperty’

其他错误,我认为现在不那么重要 . 我不知道你是否会使用自定义属性 . 你能否向我解释 kind 错误消息以及如何使用自定义属性?我找不到关于这个主题的任何文档(我理解) .

1 回答

  • 4

    看看这段代码,我相信它解释了一些自己的东西:

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/properties.hpp>
    #include <iostream>
    
    namespace bgl = boost;
    
    struct EdgeInfo
    {
        int weight;
        float e1;
        float e2;
    };
    
    struct EdgeInfoPropertyTag
    {
        typedef bgl::edge_property_tag kind;
        static std::size_t const num; // ???
    };
    
    std::size_t const EdgeInfoPropertyTag::num = (std::size_t)&EdgeInfoPropertyTag::num;
    
    typedef bgl::property<EdgeInfoPropertyTag, EdgeInfo> edge_info_prop_type;
    typedef bgl::adjacency_list<bgl::vecS, bgl::vecS, bgl::bidirectionalS,
        bgl::no_property, edge_info_prop_type> Graph;
    typedef bgl::graph_traits<Graph>::vertex_descriptor vertex_descr_type;
    typedef bgl::graph_traits<Graph>::edge_descriptor edge_descr_type;
    
    int
    main ()
    {
        Graph g;
        vertex_descr_type u, v;
        u = add_vertex (g);
        v = add_vertex (g);
        EdgeInfo props;
        props.weight = 3;
        std::pair<edge_descr_type, bool> result = add_edge (u, v, props, g);
    
        EdgeInfo p = get (EdgeInfoPropertyTag (), g, result.first);
        std::cout << "weight: " << p.weight << std::endl;
    }
    

    您需要了解BGL所基于的概念 .

    通过这种方式,您可以将任何类型的值悬挂在边缘上(对于顶点也是如此) . 你也可以使用预定义的属性类型,比如 edge_weight_tedge_name_t .

    另请参阅有关custom edge properties的BGL文档 .

相关问题