BGL 添加具有多个属性的边

BGL Adding an edge with multiple properties

本文关键字:属性 添加 BGL      更新时间:2023-10-16

我想让所有边缘都具有属性、重量和容量。 我发现BGL已经定义了这两个。 所以我为图形定义了边和顶点属性

 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

如果你看一下boost::p roperty的实现,你会发现属性值不能以这种方式初始化。即便如此,您(weight, capacity)的语法无论如何都是无效的,因此,如果可以像这样初始化属性,它将编写EdgeProperty prop = EdgeProperty(weight, capacity);或只是EdgeProperty prop(weight, capacity);。但是,同样,这是行不通的。从技术上讲,这是您需要初始化属性值的方式:

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

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

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

当然,更好的选择是使用捆绑属性而不是旧的 boost::p roperty 链。

正确的形式是:

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