2015-03-02 42 views
1

複製邊緣及其頂點和屬性我想從dataG.front()的頂點和屬性複製邊緣,並將其添加到testg,我嘗試了在「訪問捆綁屬性」部分中找到的內容http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/bundles.html,但它不適合我。 PS:dataG是一個圖的向量。使用BOOST

typedef std::pair<edge_iter, edge_iter> edge_pair; 
Graph testg; 
if (!dataG.empty()) 
{ 
    auto const& gr = dataG.front();   
    for (edge_pair ep = edges(gr); ep.first != ep.second; ++ep.first) //ep edge number 
    { 
     auto ep = edges(gr).first; // ep edge number 

     vertex_t from = source(*ep.first, gr); 
     vertex_t to = target(*ep.first, gr); 

     boost::add_vertex(gr[from], testg); 
     boost::add_vertex(gr[to], testg); 

     boost::add_edge(from, to, gr[*ep.first], testg); 

    } 
} 

邊緣屬性有效,但源和目標中存在問題。 (vertex_t和add_vertex部分),如何直接將頂點屬性添加到添加的頂點,因爲這裏有重複。

PS:瞭解更多信息這裏是完整的代碼http://pastebin.com/2iztGAa6

+0

[可能使用BOOST複製具有相鄰頂點及其屬性的邊的邊](http://stackoverflow.com/questions/28827006/copying-edges-with-adjacent-vertices-和他們的屬性使用提升) – TylerH 2017-02-07 02:47:22

回答

0

正如你注意到,頂點可能被複制,如果你「合併」源圖形的多個成一個圖形,這是尤其如此。

如果你不介意的話重新寫了一個頂點屬性(並保持最後分配的情況下,該值是不相同的所有的時間價值),你可以只使用一個屬性映射:

boost::property_map<Graph, boost::vertex_bundle_t>::type vpmap = boost::get(boost::vertex_bundle, testg); 

//so: 
vpmap[from] = gr[from]; 
vpmap[to] = gr[to]; 

話又說回來,另外還有等效訪問像這樣:

testg[from] = gr[from]; 
testg[to] = gr[to]; 

你甚至可以ADDRES個人捆綁成員:

boost::property_map<Graph, int VertexProperties::*>::type idmap = boost::get(&VertexProperties::id, testg); 
boost::property_map<Graph, int VertexProperties::*>::type labelmap = boost::get(&VertexProperties::label, testg); 
idmap[from] = gr[from].id; 
labelmap[from] = gr[from].label; 

基於此的所有示例documentation page

+0

這是正確的? 'vertex_t from = source(* ep,gr); vertex_t to = target(* ep,gr); \t boost :: property_map :: type idmap = boost :: get(&VertexProperties :: id,testg); \t \t \t boost :: property_map :: type labelmap = boost :: get(&VertexProperties :: label,testg); \t \t \t idmap [from] = gr [from] .id; \t \t \t labelmap [from] = gr [from] .label; boost :: add_edge(from,to,EdgeProperties(idmap [from],labelmap [from]),testg);' – 2015-03-02 14:53:37

+0

爲什麼在問題已被回答後,您在評論中不斷髮布無法讀取的混亂信息。我相信你可以測試它是否正確。 – sehe 2015-03-02 14:54:17