以下内容使用boost.1.46.1编译。
#include <boost/graph/adjacency_list.hpp>
struct Node {
int id;
};
struct Edge {
int source;
int target;
int weight;
};
int main() {
/* an adjacency_list like we need it */
typedef boost::adjacency_list<
boost::setS, // edge container
boost::listS, // vertex container
boost::bidirectionalS, // directed graph
Node, Edge> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
Graph gp1;
std::cout << "Number of vertices 1: " << boost::num_vertices(gp1) << std::endl;
Vertex v1 = boost::add_vertex(gp1);
Vertex v2 = boost::add_vertex(gp1);
std::cout << "Number of vertices 2: " << boost::num_vertices(gp1) << std::endl;
gp1[v1].id = 3;
gp1[v2].id = 4;
Graph gp2(gp1);
std::cout << "Number of vertices 3: " << boost::num_vertices(gp2) << std::endl;
boost::remove_vertex(v2, gp2);
std::cout << "Number of vertices 4: " << boost::num_vertices(gp1) << std::endl;
std::cout << "Number of vertices 5: " << boost::num_vertices(gp2) << std::endl;
boost::graph_traits<Graph>::vertex_iterator it, end;
for (boost::tie( it, end ) = vertices(gp2); it != end; ++it) {
if ( gp2[*it].id == 3 ) {
boost::remove_vertex(*it, gp2);
}
}
std::cout << "Number of vertices 6: " << boost::num_vertices(gp1) << std::endl;
std::cout << "Number of vertices 7: " << boost::num_vertices(gp2) << std::endl;
return 0;
}
当使用"boost::remove_vertex(v2, gp2)"移除v2时,gp2是如何知道v2的存在的?为什么gp1的顶点数量会减少1个?
为什么在"boost::remove_vertex(*it, gp2)"处会出现分段错误,并且我该如何解决?