本文介绍了BGL添加具有多个属性的边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有所有边缘都性能,重量和容量。我发现,BGL已经他们两个已经定义。所以我定义图形边缘和顶点属性

 的typedef财产< vertex_name_t,串> VertexProperty;
 typedef的财产< edge_weight_t,INT,财产和LT; edge_capacity_t,INT> > EdgeProperty正是;
 的typedef的adjacency_list<名单,血管内皮细胞,undirectedS,VertexProperty,EdgeProperty正是>图形;

这里是我想要的边添加到图:

  172:EdgeProperty正是道具=(重量,容量);
173:的add_edge(vertex1,vertex2,道具,G);

如果我刚一间酒店,我知道这将是道具= 5;但是,有两个我感到困惑的格式。

这是我收到的错误:

  graph.cc:在函数'无效con_graph():
graph.cc:172:警告:逗号左边的操作没有任何影响


解决方案

如果你看的你会看到一个属性值不能被这种方式初始化。即使如此,语法有(重量,容量)无效反正,所以,如果有可能初始化这样的属性,它会被写入 EdgeProperty正是道具= EdgeProperty正是(重量,容量); 或只是 EdgeProperty正是道具(重量,容量); 。但同样,这是行不通的。从技术上讲,这是你需要初始化属性值的方式:

  EdgeProperty正是道具= EdgeProperty正是(重量,性能和LT; edge_capacity_t,INT>(容量));

但是,这是一种丑陋的属性的数量增加。因此,这将是更清洁的违约,构建边缘属性,然后手动设置每个属性:

  EdgeProperty正是道具;
get_property_value(道具,edge_weight_t)=体重;
get_property_value(道具,edge_capacity_t)=能力;

当然,更好的办法是使用捆绑的属性而​​不是旧的boost ::财产链。

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
解决方案

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.

这篇关于BGL添加具有多个属性的边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 00:11