c++ - Boost:如何删除顶点的所有出边

标签 c++ boost graph

在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/

相关文章:

c++ - 我应该使用图形库吗?

c++ - 设置 vtkLegendScaleActor 对象的位置

c++ - 在单独的线程中调用 boost::python::object 作为函数

c++ - boost bind.hpp 1.54 c++ 专用模板无法在 Solaris 上编译。有效的 C++?

c++ - "dll-path"在构建 boost 时没有作用

graph - 未加权图的聚类算法

graph - 如何将文本文件自动转换为graphviz点文件?

c++ - C++中operator()和operator<有什么区别?

c++ - 理想的缓冲区大小是多少?

c++ - 在大数据上使用 boostfiltering_streambuf