我对如何使用 boost 库实际创建图表感到困惑,我查看了示例代码,但没有任何评论解释它的作用。
如何制作图形,并在进行时添加顶点和边?
最佳答案
这是一个简单的例子,使用邻接表并执行拓扑排序:
#include <iostream>
#include <deque>
#include <iterator>
#include "boost/graph/adjacency_list.hpp"
#include "boost/graph/topological_sort.hpp"
int main()
{
// Create a n adjacency list, add some vertices.
boost::adjacency_list<> g(num tasks);
boost::add_vertex(0, g);
boost::add_vertex(1, g);
boost::add_vertex(2, g);
boost::add_vertex(3, g);
boost::add_vertex(4, g);
boost::add_vertex(5, g);
boost::add_vertex(6, g);
// Add edges between vertices.
boost::add_edge(0, 3, g);
boost::add_edge(1, 3, g);
boost::add_edge(1, 4, g);
boost::add_edge(2, 1, g);
boost::add_edge(3, 5, g);
boost::add_edge(4, 6, g);
boost::add_edge(5, 6, g);
// Perform a topological sort.
std::deque<int> topo_order;
boost::topological_sort(g, std::front_inserter(topo_order));
// Print the results.
for(std::deque<int>::const_iterator i = topo_order.begin();
i != topo_order.end();
++i)
{
cout << tasks[v] << endl;
}
return 0;
}
我同意 boost::graph 文档可能令人生畏,但值得拥有 look .
我不记得打印书的内容是否相同,我怀疑它在眼睛上更容易一些。我实际上是从书中学会了使用 boost:graph 的。不过,学习曲线可能会感觉非常陡峭。我引用的书和评论可以找到here .
关于c++ - 使用 C++ Boost 的图形库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8812466/