簡體   English   中英

我正在嘗試通過Boost圖形庫從Graphviz DOT文件讀取圖形。 如何讀取存儲在數組中的未知數量的屬性?

[英]I am trying to read a graph from Graphviz DOT file by Boost graph library. how to read unknown number of properties which is stored in an array?

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);

如何從Graphviz DOT文件中通過數組屬性[SIZE]讀取未知屬性,或者甚至在不知道其大小的情況下也不知道? 例如,以下代碼總是錯誤的:dp.property(“ Attribute0”,boost :: get(&DotVertex :: Attribute [0],graph_t))

您可以使用任何屬性映射。 但是您需要一個有效的。

您所有的財產地圖均無效。 主要是因為graph_t命名了一個類型(使用graphviz ?)。

最后一個是因為沒有成員Attribute[0] 只有Attribute ,它是一個數組。 因此,要完全使用它,您需要添加一個轉換。 也可以看看

DEMO

生活在Coliru

#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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM