gpt4 book ai didi

c++ - 如何输出graphml格式的有向图?

转载 作者:行者123 更新时间:2023-11-28 03:19:11 25 4
gpt4 key购买 nike

如何将下图输出到 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;

最佳答案

这是不使用 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 .

关于c++ - 如何输出graphml格式的有向图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15967404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com