use*_*821 5 c++ boost dot graphviz
我试图从Graphviz DOT文件中读取图形.我对Vertex的两个属性感兴趣 - 它的id和外围.A还想加载图形标签.
我的代码看起来像这样:
struct DotVertex {
std::string name;
int peripheries;
};
struct DotEdge {
std::string label;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
DotVertex, DotEdge> graph_t;
graph_t graphviz;
boost::dynamic_properties dp;
dp.property("node_id", boost::get(&DotVertex::name, graphviz));
dp.property("peripheries", boost::get(&DotVertex::peripheries, graphviz));
dp.property("edge_id", boost::get(&DotEdge::label, graphviz));
bool status = boost::read_graphviz(dot, graphviz, dp);
Run Code Online (Sandbox Code Playgroud)
我的示例DOT文件如下所示:
digraph G {
rankdir=LR
I [label="", style=invis, width=0]
I -> 0
0 [label="0", peripheries=2]
0 -> 0 [label="a"]
0 -> 1 [label="!a"]
1 [label="1"]
1 -> 0 [label="a"]
1 -> 1 [label="!a"]
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到异常"Property not found:label".我究竟做错了什么?
您没有为"label"定义(动态)属性映射.
使用ignore_other_properties或定义它:)
在样品下面,使用ignore_other_properties需要防止rankdir(图表属性)和width,style(顶点属性):
#include <boost/graph/graphviz.hpp>
#include <libs/graph/src/read_graphviz_new.cpp>
#include <iostream>
struct DotVertex {
std::string name;
std::string label;
int peripheries;
};
struct DotEdge {
std::string label;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
DotVertex, DotEdge> graph_t;
int main() {
graph_t graphviz;
boost::dynamic_properties dp(boost::ignore_other_properties);
dp.property("node_id", boost::get(&DotVertex::name, graphviz));
dp.property("label", boost::get(&DotVertex::label, graphviz));
dp.property("peripheries", boost::get(&DotVertex::peripheries, graphviz));
dp.property("label", boost::get(&DotEdge::label, graphviz));
bool status = boost::read_graphviz(std::cin, graphviz, dp);
return status? 0 : 255;
}
Run Code Online (Sandbox Code Playgroud)
哪个成功运行
有关使用的更多说明,请参见此处dynamic_properties:Boost :: Graph中的read_graphviz(),传递给构造函数