[英]BGL: Using bundled properties to store vertex descriptor of another vertex
我正在嘗試使用boost::adjacency list
和捆綁的屬性來創建樹形圖,以存儲每個頂點的父級,我想以一種在不刪除頂點的情況下不會使它們失效的方式存儲頂點描述符,因此我使用boost::listS
,代碼應該看起來像這樣
// custom vertex for tree graphs
struct tree_vertex_info{
// descriptor of parent vertex
boost::graph_traits<Tree>::vertex_descriptor parent_in_tree;
};
// the tree graph type
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS
tree_vertex_info, boost::no_property, graph_info> Tree;
但這是行不通的,因為必須在結構定義之后定義Tree。 還有其他方法可以使用捆綁屬性嗎? 我以為我可以使用int變量而不是vertex_descriptor類型來存儲vertex_descriptor,但是由於我使用boost::listS
來存儲它們,所以我不確定是否可以。
從理論上講,您可能會遇到以下情況:
struct tree_vertex_info; // forward-declaration
typedef boost::adjacency_list<
boost::listS, boost::listS, boost::directedS,
tree_vertex_info, boost::no_property, graph_info> Tree;
struct tree_vertex_info {
boost::graph_traits<Tree>::vertex_descriptor parent_in_tree;
};
但是,這將需要boost::adjacency_list
類模板支持不完整的類型 (這是tree_vertex_info
具有的功能,而只有一個前向聲明,直到編譯器達到它的完整聲明為止)。 據我所知, boost::adjacency_list
類不支持不完整的類型(並且我知道該實現相當多,而且我認為它不起作用),並且肯定不能保證支持它們。
我實際上正在開發boost::adjacency_list
的新版本,我將其稱為boost::adjacency_list_BC
因為它基於Boost.Container容器,並且將支持不完整的類型。 但是,它在beta階段仍然很友好(請在此處或此處 ),並且最新版本的Boost.Container似乎破壞了我仍然需要弄清楚的內容。 順便說一下,我也有許多BGL樹數據結構以及樹的新BGL概念和特征(因為您似乎正在實現一種樹)。
另外,如果您這樣做的動機確實是您所擁有的(“樹中的父樹”),那么您應該在adjacency_list
使用boost::bidirectionalS
adjacency_list
以便能夠從子頂點到其父頂點(即boost::bidirectionalS
意味着什么,您將獲得BidirectionalGraph )。
最后,要真正解決您所處的這種情況,必須使用類型擦除技術。 一種簡單的現成方法是使用boost::any
刪除vertex_descriptor的類型,如下所示:
struct tree_vertex_info{
// descriptor of parent vertex
boost::any parent_in_tree;
};
// the tree graph type
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS
tree_vertex_info, boost::no_property, graph_info> Tree;
只需查找Boost.Any即可獲得使用說明。
我以為可以使用int變量而不是vertex_descriptor類型來存儲vertex_descriptor,但是由於我使用listS來存儲它們,因此不確定是否可以。
你不能。 您不能特別依賴vertex_descriptor類型(例如,您不能假設它是整數類型,更不用說“ int”了)。 我碰巧知道vertex_descriptor通常是迭代器類型(例如std::list<T>::iterator
)或大小類型(例如std::size_t
或std::vector<T>::size_type
),但這是您不應該也不可以依賴的實現細節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.