加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – boost :: write_graphviz – 如何水平生成图形

发布时间:2020-12-16 07:21:41 所属栏目:百科 来源:网络整理
导读:我正在尝试生成一个.dot,它显示一个带有Boost Graph Library的水平图. 创建图表时我的代码如下所示: struct VertexP { std::string tag;};struct EdgeP { std::string symbol;};struct GraphP{ std::string orientation;};typedef boost::adjacency_listboo
我正在尝试生成一个.dot,它显示一个带有Boost Graph Library的水平图.

创建图表时我的代码如下所示:

struct VertexP {
    std::string tag;
};

struct EdgeP {
    std::string symbol;
};

struct GraphP{
    std::string orientation;
};

typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::directedS,VertexP,EdgeP,GraphP> Graph;

GraphP property;
property.orientation = "LR";
Graph graph(property);
// Then fill the graph

我用来生成.dot文件的代码是这样的:

Graph g = creator.AutomatonToGraph(&automaton);
ofstream dot_file("automaton.dot");
dynamic_properties dp;
dp.property("node_id",get(&VertexP::tag,g));
dp.property("label",get(&EdgeP::symbol,g));

write_graphviz_dp(dot_file,g,dp);

这会将.dot文件与节点和边缘标签完美地写在一起,但我的问题是我想将rankdir = LR graph属性添加到输出文件中.我尝试过:

Graph g = creator.AutomatonToGraph(&automaton);
ofstream dot_file("automaton.dot");
dynamic_properties dp;
dp.property("node_id",g));
dp.property("rankdir",get(&GraphP::orientation,dp);

但是从这开始我得到了很大的错误:

/src/lab2.cc:48:55:   required from here
/usr/include/boost/graph/detail/adjacency_list.hpp:2585:29: error: forming reference to void
         typedef value_type& reference;
                             ^~~~~~~~~

我真的很新用BGL,我做错了什么?

解决方法

阅读dynamic_graph_properties_writer的实现,我想你应该这样做

dp.property("rankdir",boost::make_constant_property<Graph*>(std::string("LR")));

对于动态检索,您可以使用函数属性映射:(map set/get requests into C++ class/structure changes):

#include <boost/property_map/function_property_map.hpp>

    dp.property("rankdir",boost::make_function_property_map<Graph*>([](Graph const* g) { return g->m_property->orientation; }));

现场演示

见它Live On Wandbox

#include <boost/graph/adj_list_serialize.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/function_property_map.hpp>
#include <fstream>
using namespace boost;

struct VertexP { std::string tag; };
struct EdgeP { std::string symbol; };
struct GraphP { std::string orientation; };

typedef adjacency_list<vecS,vecS,directedS,GraphP> Graph;

int main() {
    Graph g(GraphP{"LR"});
    // Then fill the graph
    add_edge(
        add_vertex(VertexP{ "tag1" },g),add_vertex(VertexP{ "tag2" },EdgeP{ "symbol" },g
    );

    {
        std::ofstream dot_file("automaton.dot");
        dynamic_properties dp;
        dp.property("node_id",g));
        dp.property("label",g));
        dp.property("rankdir",boost::make_constant_property<Graph*>(std::string("LR")));
        dp.property("dummy",boost::make_function_property_map<Graph*>([](Graph const* g) { return g->m_property->orientation; }));

        write_graphviz_dp(dot_file,dp);
    }
}

写道

digraph G {
dummy=LR;
rankdir=LR;
tag2 [label=tag2];
tag1 [label=tag1];
tag1->tag2  [label=symbol];
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读