使用boost库中的remove_vertex函数来移除无向图或有向图中的顶点。示例代码如下:
#include
typedef boost::adjacency_list Graph; // 无向图
//typedef boost::adjacency_list Graph; // 有向图
typedef boost::graph_traits::vertex_descriptor Vertex;
int main()
{
Graph g(5); //定义5个顶点的无向图
boost::add_edge(0, 1, g); // 添加无向边
boost::add_edge(1, 2, g);
boost::add_edge(2, 3, g);
boost::add_edge(3, 4, g);
// 删除顶点2及其边
boost::clear_vertex(2, g);
boost::remove_vertex(2, g);
return 0;
}
执行结果: 删除顶点2及其相邻边。