[英]BGL Adding an edge with multiple properties
我希望所有的邊緣都具有屬性,重量和容量。 我發現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);
如果我只有一個屬性,我知道它將是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));
但隨着物業數量的增加,這有點難看。 因此,默認構造edge-property然后手動設置每個單獨的屬性會更簡潔:
EdgeProperty prop;
get_property_value(prop, edge_weight_t) = weight;
get_property_value(prop, edge_capacity_t) = capacity;
當然,更好的選擇是使用捆綁屬性而不是舊的boost :: property鏈。
正確的形式是:
EdgeProperty prop;
get_property_value(prop, edge_weight) = weight;
get_property_value(prop, edge_capacity) = capacity;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.