在boost graph library中,remove_edge会使边迭代器失效,那么移除一个顶点的所有出边的正确方法是什么,例如,我试图移除顶点0的所有出边。下面的代码片段无法正常工作。
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);
}
最佳答案
您可以在源顶点上为出边调用 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)
Removes all edges to and from vertex u. The vertex still appears in the vertex set of the graph.
对描述符和迭代器稳定性的影响与为所有以 u 作为源或目标的边调用 remove_edge() 的影响相同。
-
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.
对描述符和迭代器稳定性的影响与为所有以 u 为源的边调用 remove_edge() 的影响相同。
此操作不适用于无向图(请改用 clear_vertex())。
-
void clear_in_edges(vertex_descriptor u, adjacency_list& g)
如果你必须支持任何 MutableGraph ,只有 clear_vertex
。
关于c++ - Boost:如何删除顶点的所有出边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29931231/