【问题标题】:write_graphviz does not work when vertex has property当顶点具有属性时 write_graphviz 不起作用
【发布时间】:2020-02-16 16:03:22
【问题描述】:

我正在学习 boost::graph 并尝试将 name 属性添加到 Vertex。但是,在这样做之后,write_graphviz 不会保存任何内容(没有此属性,它可以工作)。

头文件:

struct VertexProps
{
    std::string name;
};

struct EdgeProps
{
    double weight;
};

using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProps, EdgeProps>;
using Vertex = boost::adjacency_list<>::vertex_descriptor;
using Edge = std::pair<boost::adjacency_list<>::edge_descriptor, bool>;
using EdgeList = std::pair<boost::adjacency_list<>::edge_iterator,
    boost::adjacency_list<>::edge_iterator>;

Cpp 文件:

Vertex vertex = boost::add_vertex({std::move(vertexName)}, g);

std::filebuf fb;
fb.open("output.txt", std::ios::out);
std::ostream os(&fb);
write_graphviz(os, g, boost::make_label_writer(get(&VertexProps::name, g)), boost::make_label_writer(get(&EdgeProps::weight, g)));
fb.close();

该文件应该包含图表,但我只能看到:

digraph G {
}

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    不清楚你有什么不同,但这里有一个基于上述内容的独立示例:

    Live On Wandbox

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/graphviz.hpp>
    
    struct VertexProps { std::string name; };
    struct EdgeProps { double weight; };
    
    using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProps, EdgeProps>;
    
    int main(){
        Graph g;
    
        auto vertex = add_vertex({"MyVertexName"}, g);
    
        write_graphviz(std::cout, g,
            make_label_writer(get(&VertexProps::name, g)),
            make_label_writer(get(&EdgeProps::weight, g)));
    }
    

    打印出来的

    digraph G {
    0[label=MyVertexName];
    }
    

    【讨论】:

    • 啊,谢谢。我不知道为什么它以前不起作用
    猜你喜欢
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2016-05-28
    • 1970-01-01
    相关资源
    最近更新 更多