c++ - 使用 C++ Boost 的图形库

标签 c++ boost

我对如何使用 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/

相关文章:

C++:转发参数包和 std::tuple/array 的总和

c++ - std::make_shared()、std::weak_ptr 和循环引用

c++ - C Map 插入迭代器绑定(bind)

c++ - Boost::MPL 的实际使用示例?

c++ - Clang 输出错误 "no matching construct for initialization"

C++概念帮助,指针

c++ - 使用 Visual Studio Compiler 分析内联 C++ 函数

c++ - 包含常量成员的 POD 结构

ios - 如何使用 XCode 5 和 iOS7 在 Mountain Lion 上安装 Boost 库

OSX 10.6.6 上的 c++ boost 1_46_1。不链接