![](/img/trans.png)
[英]BGL adjacency_list<listS, listS>::vertex_descriptor evaluates to void*
[英]how to access BGL's vertex_descriptor as an int
我有一個鄰接列表,如下所示。 此時我需要訪問vertex_descriptor作為int類型。 我該怎么做tvertex source = ...; int source_as_int = ???source???
tvertex source = ...; int source_as_int = ???source???
我記得之前碰到過同樣的問題並解決了它,但是不記得BGL文檔如何使用它作為參考是無用的,他們應該試着看看並向Javadocs學習。
另一種可能性是使用vertex_descriptor
類型的一個可能的成員函數或者為此目的使用一些全局BGL函數...一個人永遠不知道在哪里尋找它,他們似乎在制作全局函數或成員函數之間隨機選擇,如果你問我,直覺設計完全失敗了。
typedef adjacency_list_traits<setS, setS, bidirectionalS> ttraits;
typedef adjacency_list<setS, setS, bidirectionalS,
// vertex properties
property<vertex_color_t, default_color_type>,
// edge properties
property<edge_capacity_t, int,
property<edge_residual_capacity_t, int,
property<edge_reverse_t, ttraits::edge_descriptor> > >, no_property, vecS> tbgl_adjlist_bidir;
typedef graph_traits<tbgl_adjlist_bidir>::vertex_descriptor tvertex;
typedef graph_traits<tbgl_adjlist_bidir>::edge_descriptor tedge;
typedef property_map<tbgl_adjlist_bidir, edge_capacity_t>::type tedge_capacity_map;
typedef property_map<tbgl_adjlist_bidir, edge_reverse_t>::type treverse_edge_map;
typedef property_map<tbgl_adjlist_bidir, vertex_color_t>::type tvertex_color_map;
typedef graph_traits<tbgl_adjlist_bidir>::out_edge_iterator tout_edge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::in_edge_iterator tin_edge_iterator;
好的,我明白了。 添加頂點屬性vertex_index_t
可以解決問題。 然后我可以像這樣訪問頂點的int索引:
typedef adjacency_list_traits<setS, vecS, bidirectionalS> ttraits;
typedef adjacency_list<setS, vecS, bidirectionalS,
// vertex properties
property<vertex_index_t, int,
property<vertex_color_t, default_color_type> >,
// edge properties
property<edge_capacity_t, int,
property<edge_residual_capacity_t, int,
property<edge_reverse_t, ttraits::edge_descriptor> > >, no_property, vecS> tbgl_adjlist_bidir;
typedef graph_traits<tbgl_adjlist_bidir>::vertex_descriptor tvertex;
typedef graph_traits<tbgl_adjlist_bidir>::edge_descriptor tedge;
typedef property_map<tbgl_adjlist_bidir, edge_capacity_t>::type tedge_capacity_map;
typedef property_map<tbgl_adjlist_bidir, edge_reverse_t>::type treverse_edge_map;
typedef property_map<tbgl_adjlist_bidir, vertex_color_t>::type tvertex_color_map;
typedef property_map<tbgl_adjlist_bidir, vertex_index_t>::type tvertex_index_map;
typedef graph_traits<tbgl_adjlist_bidir>::vertex_iterator tvertex_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::edge_iterator tedge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::out_edge_iterator tout_edge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::in_edge_iterator tin_edge_iterator;
然后我像這樣使用它:
tbgl_adjlist_bidir bgl_adjlist_bidir;
// ...
tvertex_index_map indices = get(vertex_index, bgl_adjlist_bidir);
// ...
tvertex source;
// ...
int source_as_int = indices[source];
vertex_descriptor
的類型取決於adjacency_list
的VertexListS
-template參數的基礎結構。 我知道的唯一情況是,當VertexList-Type是vecS
時,描述符是int
。 但請記住:如果選擇vecS
作為VertexList-Type,則所有(存儲的)描述符都會變為無效,當您更改圖形的結構時(如Iterator和Descriptor Stability / Invalidation中所述 )。
我是否會對您使用自定義頂點和邊緣類型感興趣? 在您自己的代碼中使用它們會更容易 。 至於調用BGL算法,您可以使用Bundled Properties 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.