【发布时间】:2014-09-09 14:08:22
【问题描述】:
问题可能与: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
由于文章中讨论的原因而不起作用。我还浏览了各种示例,包括来自 boost graph 纪录片:http://www.boost.org/doc/libs/1_36_0/libs/graph/example/family-tree-eg.cpp(都是“手工”制作的),
或来自“Boost Graph Library”p.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 文件创建图表,我将不胜感激。
在此先感谢你们,并致以最良好的祝愿。
【问题讨论】: