Ser*_*e C 32 c++ boost graph boost-graph
我正在寻找一种通过使用键而不是顶点引用本身来访问顶点属性的方法.例如,如果我有
class Data
{
public:
std::string name;
unsigned int value;
};
typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, Data > Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
Run Code Online (Sandbox Code Playgroud)
而不是使用
Vertex vertex1 = boost::add_vertex( g );
g[vertex1].name = "Alpha";
g[vertex1].value = 10;
Run Code Online (Sandbox Code Playgroud)
我想拥有
g["Alpha"].name = "Alpha";
g["Alpha"].value = 10;
Run Code Online (Sandbox Code Playgroud)
是否存在现成的机制?
Ser*_*e C 33
我想我已经找到了这样的机制.它被称为labeled_graph,是BGL的一部分.可以使用预定义的包装程序labeled_graph,而不是使用adjacency_list:
typedef boost::labeled_graph<
boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, Data >,
std::string
> Graph;
Run Code Online (Sandbox Code Playgroud)
在定义这样的图形之后,可以通过以下方式访问顶点:
Graph g;
boost::add_vertex( "Alpha", g );
g["Alpha"].name = "Alpha";
g["Alpha"].value = 10;
boost::add_vertex( "Beta", g );
g["Beta"].name = "Beta";
g["Beta"].value = 20;
boost::add_edge_by_label( "Alpha", "Beta", g );
Run Code Online (Sandbox Code Playgroud)
这样做的副作用是需要使用graph()成员函数来使一些算法工作:
std::vector< Graph::vertex_descriptor > container;
boost::topological_sort( g.graph(), std::back_inserter( container ) ) ;
Run Code Online (Sandbox Code Playgroud)
出于某种原因,label_graph未在BGL文档中描述,但它出现在示例文件夹中.
塞尔,谢谢你的回复