2012-02-13 103 views
5

如何將類型adjacency_list的圖形複製到另一個類型爲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(); 

回答

6

您試過copy_graph


很難知道問題是什麼,而不會看到錯誤,但如果要我猜,我首先要確保你提供一個vertex_index地圖copy_graph因爲它是不可用時默認使用setS用於頂點存儲。根據你的earlier question,看起來你已經弄清楚了,所以我們只需要把它們放在一起。

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(); 
+0

對於copy_graph,據說圖類型必須是VertexListGraph的模型。就我而言,我說過它是一個adjacency_list。 – shn 2012-02-13 14:56:28

+1

@ user995434但是adjacency_list是VertexAndEdgeListGraph的一個模型,它是VertexListGraph的一種改進。因此,adjacency_list是VertexListGraph的模型。 – 2012-02-13 15:25:25

+0

你可以給我一個使用copy_graph()的小例子來說明我想做什麼嗎?它總是給我編譯錯誤。提前致謝。 – shn 2012-02-13 21:20:41

相關問題