2015-03-02 41 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