【问题标题】:Boost: how to remove all the out-edges for a vertexBoost:如何删除顶点的所有外边
【发布时间】:2015-04-28 22:43:07
【问题描述】:

在boost图形库中,remove_edge会使边迭代器失效,那么删除一个顶点的所有出边的正确方法是什么,例如,我试图删除顶点0的所有出边。下面的代码 sn-p 不能正常工作。

Graph G(N);
graph_traits <Graph>::out_edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = out_edges(0, G); ei != ei_end; ++ei) {
   vertex targ = target(*ei, G);
   cout << "target vtx = " << targ << endl;

   if ( edge(0, targ, G).second != 0 )
     remove_edge(0, targ, G);
}

【问题讨论】:

    标签: c++ boost graph


    【解决方案1】:

    您可以在源顶点上调用 clear_out_edges 以获取出边 (http://www.boost.org/doc/libs/1_58_0/libs/graph/doc/adjacency_list.html)

    • void clear_vertex(vertex_descriptor u, adjacency_list& g)
      

      删除顶点 u 的所有边。顶点仍然出现在图形的顶点集中。

      对描述符和迭代器稳定性的影响与对所有以 u 作为源或目标的边调用 remove_edge() 的影响相同。

    • void clear_out_edges(vertex_descriptor u, adjacency_list& g)
      

      从顶点 u 中删除所有出边。顶点仍然出现在图形的顶点集中。

      对描述符和迭代器稳定性的影响与对所有以 u 作为源的边调用 remove_edge() 的影响相同。

      此操作不适用于无向图(使用 clear_vertex() 代替)。

    • void clear_in_edges(vertex_descriptor u, adjacency_list& g)
      

    如果非要支持anyMutableGraph,那就只有clear_vertex了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 2017-12-05
      相关资源
      最近更新 更多