C++ 将图形(邻接列表)复制到另一个图形
如何将一个邻接列表类型的图形复制到另一个邻接列表类型的图形C++ 将图形(邻接列表)复制到另一个图形,c++,boost,copy,boost-graph,C++,Boost,Copy,Boost Graph,如何将一个邻接列表类型的图形复制到另一个邻接列表类型的图形 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.cle
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();
typedef邻接列表MyGraph;
MyGraph g1、g2;
//正在处理g1:添加顶点和边。。。
//正在处理g2:添加一些顶点和边。。。
g1.clear();
g1=g2//这会导致执行错误(异常)
g1=MyGraph(g2);//这也会导致执行错误
g2.clear();
你试过了吗
在没有看到错误的情况下,很难知道问题出在哪里,但如果我不得不猜测,我首先要确保您提供了一个
顶点索引
映射到复制图
,因为在使用集合
进行顶点存储时,默认情况下它不可用。根据你的数据,看起来你已经解决了这个问题,所以我们只需要把所有的问题结合起来
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();
typedef邻接列表MyGraph;
typedef MyGraph::顶点描述符NodeID;
类型定义映射索引映射;
IndexMap-mapIndex;
关联属性映射propmapIndex(mapIndex);
MyGraph g1、g2;
//正在处理g1:添加顶点和边。。。
//正在处理g2:添加一些顶点和边。。。
int i=0;
所有顶点的BGL_(v、g2、MyGraph)
{
put(propmaindex,v,i++);
}
g1.clear();
复制图(g2,g1,顶点索引图(propmapIndex));
g2.clear();
对于复制图,据说图形类型必须是VertexListGraph的模型。在我的例子中,我说过它是一个邻接列表。@user995434但是邻接列表是VertexAndEdgeListGraph的一个模型,它是VertexListGraph的一个改进。因此,邻接列表是VertexListGraph的一个模型。您能给出一个使用copy_graph()的示例吗?它总是给我编译错误。提前谢谢。我知道这是旧的。我想知道是否需要为顶点和边属性定义复制构造函数。我最近需要复制一个图,复制构造函数对我来说工作得很好。