gpt4 book ai didi

c++ - BGL 添加具有多个属性的边

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:17 24 4
gpt4 key购买 nike

我想让所有边都具有属性、重量和容量。我发现 BGL 已经定义了这些。所以我为图定义了 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

最佳答案

如果您查看 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 链。

关于c++ - BGL 添加具有多个属性的边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com