shn*_*shn 5 c++ boost copy boost-graph
如何将adjacency_list类型的图形复制到另一个类型为adjacency_list的图形中?
typedef adjacency_list<setS, setS, undirectedS, NodeDataStruct, EdgeDataStruct> MyGraph;
MyGraph g1, g2;
// processing g1: adding vertices and edges ...
// processing g2: adding some vertices and edges ...
g1.clear();
g1 = g2 // this gives an execution error (exception)
g1 = MyGraph(g2); // this also gives an execution error
g2.clear();
Run Code Online (Sandbox Code Playgroud)
你试过copy_graph吗?
很难知道问题是什么而没有看到错误,但如果我不得不猜测,我首先要确保你提供一个vertex_index地图,copy_graph因为它在你setS用于顶点存储时默认不可用.根据你之前的问题,看起来你已经弄清楚了,所以我们只需要将它们整合在一起.
typedef adjacency_list<setS, setS, undirectedS, NodeDataStruct, EdgeDataStruct> MyGraph;
typedef MyGraph::vertex_descriptor NodeID;
typedef map<NodeID, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);
MyGraph g1, g2;
// processing g1: adding vertices and edges ...
// processing g2: adding some vertices and edges ...
int i=0;
BGL_FORALL_VERTICES(v, g2, MyGraph)
{
put(propmapIndex, v, i++);
}
g1.clear();
copy_graph( g2, g1, vertex_index_map( propmapIndex ) );
g2.clear();
Run Code Online (Sandbox Code Playgroud)