BGL Adding an edge with multiple properties

∥☆過路亽.° 提交于 2019-12-06 03:33:05

问题


I want to have all edges have to properties, weight and capacity. I found that BGL has these both already defined. So I define Edge and Vertex properties for the Graph

 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;

Here is where I am trying to add the edges to the graph:

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

If I had just 1 property I know it would be prop = 5; However, with two I am confused about the formatting.

Here is the error I am receiving:

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

回答1:


If you look at the implementation of boost::property you'll see that a property value cannot be initialized this way. And even then, the syntax you have (weight, capacity) is not valid anyways, so, if it was possible to initialize the property like that, it would be written EdgeProperty prop = EdgeProperty(weight, capacity); or just EdgeProperty prop(weight, capacity);. But, again, that won't work. Technically, this is the way you would need to initialize the property value:

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

But this is kind of ugly as the number of properties increase. So, it would be cleaner to default-construct the edge-property and then manually set each individual property:

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

Of course, the better alternative is to use bundled properties instead of the older boost::property chains.




回答2:


The correct form is:

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


来源:https://stackoverflow.com/questions/11277820/bgl-adding-an-edge-with-multiple-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!