简体   繁体   中英

Boost: how to remove all the out-edges for a vertex

In boost graph library, remove_edge will invalidate the edge iterator, so what is the correct way to remove all the out-edge of a vertex, for example, I am trying to remove all the out-edge of vertex 0. The code snippet in below does not work properly.

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);
}

You would call clear_out_edges on the source vertex for the 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)

    Removes all edges to and from vertex u. The vertex still appears in the vertex set of the graph.

    The affect on descriptor and iterator stability is the same as that of invoking remove_edge() for all of the edges that have u as the source or target.

  •  void clear_out_edges(vertex_descriptor u, adjacency_list& g)

    Removes all out-edges from vertex u. The vertex still appears in the vertex set of the graph.

    The affect on descriptor and iterator stability is the same as that of invoking remove_edge() for all of the edges that have u as the source.

    This operation is not applicable to undirected graphs (use clear_vertex() instead).

  •  void clear_in_edges(vertex_descriptor u, adjacency_list& g)

If you have to support any MutableGraph , there is only clear_vertex .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM