问题可能与以下内容有关:Using boost graph library: how to create a graph by reading edge lists from file
但答案并没有真正帮助我 .
我想使用boost的adjacency_list类创建一个图形 . 数据位于包含两列的.txt文件中 .
样品:
5 14
7 2
3 18
21 207
...
如果我能做到以下几点会很棒:
std::ifstream data("data.txt");
typedef adjacency_list<> Graph;
Graph g;
while (data >> nodeA >> nodeB){
add_edge(nodeA, nodeB, g);
}
正如这里已经讨论的那样:boost graph understanding vertex creation behaviour
由于文章中讨论的原因不起作用 . 我还经历过各种例子,包括来自增强图纪录片:http://www.boost.org/doc/libs/1_36_0/libs/graph/example/family-tree-eg.cpp(全部是"hand"),
或者从“Boost Graph Library”一书第44页(我似乎无法编译) . 因此,经过几天的反复试验并查看上述来源后,我仍然无法弄清楚如何从文件中正确创建图表 .
理想情况下,我想继续如下:
while (data >> nodeA >> nodeB){
graph_traits<Graph>::vertex_descriptor *newVertex = new graph_traits<Graph>::vertex_descriptor;
newVertex = vertex(nodeA,g);
graph_traits<Graph>::vertex_descriptor *otherVertex = new graph_traits<Graph>::vertex_descriptor;
otherVertex = vertex(nodeB,g);
add_edge(newVertex, otherVertex,g);
}
类似于:www.boost.org/doc/libs/1_56_0/libs/graph/example/undirected_adjacency_list.cpp
但是我收到了编译错误 .
无论如何,我真的很感激任何帮助,以弄清楚如何从txt文件创建图形 .
谢谢你们提前和最良好的祝愿 .
1 回答
我没有看到一个直接的问题 . 除非您的顶点非常稀疏(例如,您有一个节点ID,例如99,999,999,999,并且memery使用会造成问题) .
否则,你也可以
保留
id -> vertex_descriptor
的(bi) Map使用顶点属性doc