我正在尝试通过 Boost 图形库从 Graphviz DOT 文件中读取图形.如何读取存储在数组中的未知数量的属性


int const SIZE=10;
struct DotVertex {
    int Attribute[SIZE];
};
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,        graph_t));
    dp.property("Attribute0", boost::get(&DotVertex::Attribute[0],  graph_t));
    std::ifstream dot("graphnametest2.dot");
     boost::read_graphviz(dot,  graph_t, dp);

如何通过数组属性[SIZE]从Graphviz DOT文件中读取未知属性,或者即使不知道它的大小?例如,以下代码始终是错误的:dp.property("Attribute0", boost::get(&DotVertex::Attribute[0], graph_t((

可以使用任何属性映射。但是你需要一个有效的。

您的所有媒体资源映射均无效。主要是因为graph_t命名一个类型(使用 graphviz

最后一个是因为没有会员Attribute[0]。只有Attribute,它是一个数组。因此,为了使用它,您需要添加一个转换。参见

  • 是否可以为一个图形 BOOST 提供多个边缘权重属性映射?
  • 将集/获取请求映射到类/结构更改C++

演示

住在科里鲁

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
int const SIZE=10;
struct DotVertex {
    std::string name;
    int Attribute[SIZE];
};
struct DotEdge {
    std::string label;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, DotVertex, DotEdge> graph_t;
#include <boost/phoenix.hpp>
using boost::phoenix::arg_names::arg1;
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(&DotEdge::label,  graphviz));
    auto attr_map = boost::get(&DotVertex::Attribute, graphviz);
    for (int i = 0; i<SIZE; ++i)
        dp.property("Attribute" + std::to_string(i), 
           boost::make_transform_value_property_map(arg1[i], attr_map));
    std::ifstream dot("graphnametest2.dot");
    boost::read_graphviz(dot, graphviz, dp);
}

最新更新