2013-04-12 34 views
0

如何將以下圖形輸出到graphml中?如何以graphml格式輸出有向圖?

typedef struct Vertex{ std::string name; std::string cmdb_id; 

      Vertex& operator= (const Vertex& rhs) 
      { 
        if (this == &rhs) 
          return *this; 

        name = rhs.name; 
        cmdb_id = rhs.cmdb_id; 
      } 

      bool operator< (const Vertex& rhs) const 
      { 
        return cmdb_id < rhs.cmdb_id; 
      }; 

      bool operator== (const Vertex& rhs) const 
      { 
        return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name)); 
      }; 

    }vertex_container; 

typedef struct Edge {std::string name;} edge_container; 

boost::directed_graph<vertex_container, edge_container> Graph g; 

回答

0
#include <iostream> 

#include <boost/graph/directed_graph.hpp> 
#include <boost/graph/graphml.hpp> 



typedef struct Vertex 
{ 
    std::string name; 
    std::string cmdb_id; 

    Vertex& operator= (const Vertex& rhs) 
    { 
     if (this == &rhs) 
     return *this; 

     name = rhs.name; 
     cmdb_id = rhs.cmdb_id; 
    } 

    bool operator< (const Vertex& rhs) const 
    { 
     return cmdb_id < rhs.cmdb_id; 
    }; 

    bool operator== (const Vertex& rhs) const 
    { 
     return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name)); 
    }; 

}vertex_container; 

typedef struct Edge {std::string name;} edge_container; 

typedef boost::directed_graph<vertex_container, edge_container> Graph; 

int main() 
{ 
    Graph g; 
    auto v0 = g.add_vertex({"A","1"}); 
    auto v1 = g.add_vertex({"B","2"}); 
    g.add_edge(v0,v1,{"A-B"}); 

    boost::dynamic_properties dp; 
    dp.property("vertex_name",get(&vertex_container::name,g)); 
    dp.property("vertex_cmdb_id",get(&vertex_container::cmdb_id,g)); 
    dp.property("edge_name",get(&edge_container::name,g)); 

    write_graphml(std::cout, g, dp); 

} 
+1

如果我編譯你的示例,我得到一個長的編譯器錯誤。我不會將它粘貼到這裏,它太長了。我使用gcc 4.6.3 btw。 – bayerb

2

這裏是不使用C++ 11種功能的其他答案的一個版本:

#include <iostream> 

#include <boost/graph/directed_graph.hpp> 
#include <boost/graph/graphml.hpp> 



typedef struct Vertex 
{ 
    std::string name; 
    std::string cmdb_id; 

    Vertex& operator= (const Vertex& rhs) 
    { 
     if (this == &rhs) 
     return *this; 

     name = rhs.name; 
     cmdb_id = rhs.cmdb_id; 
    } 

    bool operator< (const Vertex& rhs) const 
    { 
     return cmdb_id < rhs.cmdb_id; 
    }; 

    bool operator== (const Vertex& rhs) const 
    { 
     return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name)); 
    }; 

}vertex_container; 

typedef struct Edge {std::string name;} edge_container; 

typedef boost::directed_graph<vertex_container, edge_container> Graph; 
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor; 

int main() 
{ 
    Graph g; 
    vertex_container A, B; 
    edge_container AB; 

    A.name="A"; 
    A.cmdb_id="1"; 
    B.name="B"; 
    B.cmdb_id="2"; 
    AB.name="A-B"; 

    vertex_descriptor v0 = g.add_vertex(A); 
    vertex_descriptor v1 = g.add_vertex(B); 
    g.add_edge(v0,v1,AB); 

    boost::dynamic_properties dp; 
    dp.property("vertex_name",get(&vertex_container::name,g)); 
    dp.property("vertex_cmdb_id",get(&vertex_container::cmdb_id,g)); 
    dp.property("edge_name",get(&edge_container::name,g)); 

    write_graphml(std::cout, g, dp); 

} 

Working on g++ 4.6.3 on LWS

+0

我不明白。它不會在我的環境中編譯,除非我刪除了write_graphml ...行。升級版本是1.46.1。 – bayerb

+0

鏗鏘告訴我下面的錯誤爲write_graphml線: '/usr/include/boost/graph/directed_graph.hpp:95:11:錯誤:沒有匹配的構造函數初始化'graph_type'(又名 'adjacency_list ') :m_graph(x),m_num_vertices(x.m_num_vertices),m_num_edges(x.m_num_edges) ^〜 ' – bayerb

+0

升壓1.51.0一切都很好。 – bayerb