使用 C++ Boost 的图形库

Using C++ Boost#39;s Graph Library(使用 C++ Boost 的图形库)

本文介绍了使用 C++ Boost 的图形库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何使用 boost 库实际创建图形感到困惑,我查看了示例代码,但没有解释它的作用的注释.

I am confused about how to actually create a Graph using the boost library, I have looked at the example code and there are no comments explaining what it does.

如何制作图形,并随手添加顶点和边?

How do you make a graph, and add vertices and edges as you go?

推荐答案

这是一个简单的例子,使用邻接表并执行拓扑排序:

Here's a simple example, using an adjacency list and executing a topological sort:

#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 文档可能令人生畏,但值得拥有一个 看看.

I agree that the boost::graph documentation can be intimidating, but it's worth having a look.

我不记得印刷书的内容是否相同,我怀疑这对眼睛来说更容易一些.我实际上从书中学会了使用 boost:graph.不过,学习曲线可能会感觉很陡峭.我参考的书和评论可以在这里找到.

I can't recall if the contents of the printed book is the same, I suspect it's a bit easier on the eyes. I actually learnt to use boost:graph from the book. The learning curve can feel pretty steep though. The book I refer to and reviews can be found here.

这篇关于使用 C++ Boost 的图形库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 C++ Boost 的图形库

基础教程推荐