2014-06-19 49 views
2

升壓功能boost::graph::copy_graph在其參數描述提供頂點映射參數來提高::圖表:: copy_graph

template <class VertexListGraph, class MutableGraph> void 
copy_graph(const VertexListGraph& G, MutableGraph& G_copy, 
    const bgl_named_params<P, T, R>& params = all defaults) 

列表 UTIL/OUT: orig_to_copy(Orig2CopyMap c)其不同於拷貝頂點在頂點原來的映射。我需要這個映射!

(滾動到底在http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/copy_graph.html

如何訪問/提供這最後一個參數orig_to_copy?你能給出一個代碼示例,即爲我完成此代碼?

void doSomething(graph_t& g){ 
    graph_t g_copy; 
    copy_graph(g, g_copy, [...???...]); 
    // here I would like to access the Orig2CopyMap 
} 

回答

2

事情是這樣的:

typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_t; 
typedef boost::property_map<graph_t, boost::vertex_index_t>::type index_map_t; 

//for simple adjacency_list<> this type would be more efficient: 
typedef boost::iterator_property_map<typename std::vector<vertex_t>::iterator, 
    index_map_t,vertex_t,vertex_t&> IsoMap; 

//maps vertices of g to vertices of g_copy 
std::vector<vertex_t> isoValues(num_vertices(g));  
IsoMap mapV(isoValues.begin()); 

boost::copy_graph(g, g_copy, boost::orig_to_copy(mapV)); //means g_copy += g 
+2

我在這裏得到一個segfault。我認爲這個問題是需要在IsoMap裏面,像一個支持屬性映射的std :: map。我在這裏找到了一個工作解決方案:http://d.hatena.ne.jp/gununu/20111006/1317880754 – hooch

+0

謝謝,我錯過了屬性圖背後的容器(isoValues)。現在代碼正常工作。 –

4

發現這個解決方案在這裏:http://d.hatena.ne.jp/gununu/20111006/1317880754

void doSomething(graph_t& g){ 
    typedef graph_t::vertex_descriptor vertex_t; 
    typedef std::map<vertex_t, vertex_t> vertex_map_t; 
    vertex_map_t vertexMap; 
    // this boost type is needed around the map 
    associative_property_map<vertex_map_t> vertexMapWrapper(vertexMap); 
    graph_t g_copy; 
    copy_graph(g, g_copy, boost::orig_to_copy(vertexMapWrapper)); 
    std::cout << "mapping from copy to original: " << std::endl; 
    for(auto& iter : vertexMap){ 
     std::cout << iter.first << " -> " << iter.second << std::endl; 
    } 
}