理论上,你可以有这样的东西:
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 阶段(关注 here 或 here),最新版本的 Boost.Container 似乎已经破坏了一些我仍然需要弄清楚的东西。顺便说一句,我还有许多 BGL 树数据结构以及树的新 BGL 概念和特征(因为您似乎正在实现一种树)。
另外,如果你这样做的动机确实是你所拥有的(“树中的父级”),那么你应该在你的adjacency_list 中使用boost::bidirectionalS 以便能够从一个子顶点到它的父级(这就是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),但这是您不应该也不能依赖的实现细节。