2011-10-31 94 views
5

我開始將BGL用於某些與圖形相關的任務。我有大量的邊緣,每個邊緣有幾個屬性,其中之一是它的重量。 (所有屬性都是浮動和整數)。由於我以前從未使用過BGL(和/或類似的CPP庫),因此我對所有這些類型,類以及如何正確使用它都有些迷茫。BGL邊緣的自定義屬性

添加我的邊緣是這樣的:

struct EdgeProperty 
{ 
    int weight; 
    float e1; 
    float e2; 
}; 

typedef adjacency_list<vecS, vecS, bidirectionalS, no_property, EdgeProperty> Graph; 
... 
EdgeProperty prop; 
node1 = ...; 
node2 = ...; 
prop.e1 = ...; 
prop.e2 = ...; 
prop.weight = ...; 

add_edge(node1, node2, prop, g); 

然後,我需要後來訪問我的財產,我所試圖做的是這樣的:

property_map<Graph, EdgeProperty>::type EdgeWeightMap = get(EdgeProperty, g); 
w = get(EdgeWeightMap,some_edge); 

然而,這並不甚至編譯。它說,在錯誤消息:

error: no type named ‘kind’ in ‘struct EdgeProperty’

除其他錯誤,我現在認爲不那麼重要。我不知道這是如何使用自定義屬性。您可以請向我解釋kind錯誤消息以及如何使用自定義屬性?我找不到任何有關此主題的文檔(我瞭解)。

回答

4

看看這個代碼,相信它解釋了自己的幾件事情:

#include <boost/graph/adjacency_list.hpp> 
#include <boost/graph/properties.hpp> 
#include <iostream> 

namespace bgl = boost; 

struct EdgeInfo 
{ 
    int weight; 
    float e1; 
    float e2; 
}; 

struct EdgeInfoPropertyTag 
{ 
    typedef bgl::edge_property_tag kind; 
    static std::size_t const num; // ??? 
}; 

std::size_t const EdgeInfoPropertyTag::num = (std::size_t)&EdgeInfoPropertyTag::num; 

typedef bgl::property<EdgeInfoPropertyTag, EdgeInfo> edge_info_prop_type; 
typedef bgl::adjacency_list<bgl::vecS, bgl::vecS, bgl::bidirectionalS, 
    bgl::no_property, edge_info_prop_type> Graph; 
typedef bgl::graph_traits<Graph>::vertex_descriptor vertex_descr_type; 
typedef bgl::graph_traits<Graph>::edge_descriptor edge_descr_type; 

int 
main() 
{ 
    Graph g; 
    vertex_descr_type u, v; 
    u = add_vertex (g); 
    v = add_vertex (g); 
    EdgeInfo props; 
    props.weight = 3; 
    std::pair<edge_descr_type, bool> result = add_edge (u, v, props, g); 

    EdgeInfo p = get (EdgeInfoPropertyTag(), g, result.first); 
    std::cout << "weight: " << p.weight << std::endl; 
} 

您需要閱讀有關BGL是基於概念。

這樣,您可以將任何類型的值掛起邊緣(對於頂點也是類似的)。您也可以使用預定義類型的屬性,例如我相信edge_weight_tedge_name_t

另請參閱關於custom edge properties的BGL文檔。

+0

該鏈接幫了我很多。謝謝! – janoliver