【问题标题】:BGL Adding an edge with multiple propertiesBGL 添加具有多个属性的边
【发布时间】:2012-07-01 22:21:30
【问题描述】:

我希望所有的边缘都具有属性、重量和容量。我发现 BGL 已经定义了这两个。所以我为 Graph 定义 Edge 和 Vertex 属性

 typedef property<vertex_name_t, string> VertexProperty;
 typedef property<edge_weight_t, int, property<edge_capacity_t, int> > EdgeProperty;
 typedef adjacency_list<listS,vecS, undirectedS, VertexProperty, EdgeProperty > Graph;

这是我尝试将边添加到图表的地方:

172: EdgeProperty prop = (weight, capacity);
173: add_edge(vertex1,vertex2, prop, g);

如果我只有 1 个属性,我知道它会是 prop = 5;但是,有两个我对格式感到困惑。

这是我收到的错误:

graph.cc: In function ‘void con_graph()’:
graph.cc:172: warning: left-hand operand of comma has no effect

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    如果您查看boost::property 的实现,您会发现无法以这种方式初始化属性值。即使这样,(weight, capacity) 的语法仍然无效,因此,如果可以像这样初始化属性,它会写成 EdgeProperty prop = EdgeProperty(weight, capacity); 或只是 EdgeProperty prop(weight, capacity);。但是,这又是行不通的。从技术上讲,这是您需要初始化属性值的方式:

    EdgeProperty prop = EdgeProperty(weight, property<edge_capacity_t, int>(capacity));
    

    但是随着属性数量的增加,这有点难看。因此,默认构造边缘属性然后手动设置每个单独的属性会更干净:

    EdgeProperty prop;
    get_property_value(prop, edge_weight_t) = weight;
    get_property_value(prop, edge_capacity_t) = capacity;
    

    当然,更好的选择是使用捆绑属性而不是旧的 bo​​ost::property 链。

    【讨论】:

    • 能否举例说明最新的 boost 图形库的捆绑属性?
    【解决方案2】:

    正确的形式是:

    EdgeProperty prop;
    get_property_value(prop, edge_weight) = weight;
    get_property_value(prop, edge_capacity) = capacity;
    

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 2020-05-03
      • 2016-01-25
      • 2015-09-17
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多