簡體   English   中英

BGL中邊的自定義屬性

[英]custom properties for edges in the BGL

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

我像這樣添加邊緣:

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錯誤信息以及如何使用自定義屬性? 我找不到關於這個主題的任何文檔(我理解)。

看看這段代碼,我相信它解釋了一些自己的東西:

#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

另請參閱有關自定義邊緣屬性的 BGL文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM