首页 文章

adjexncy_list,其中VertexList与vecS不同

提问于
浏览
2

我有两个包含一些字段的结构:struct MyNodeData和struct MyEdgeData . 当我创建一个VertexList为vecS的图形时,访问顶点描述符等没有问题 . 例如:

typedef adjacency_list<setS, vecS, undirectedS, MyNodeData, MyEdgeData> Graph;

typedef Graph::vertex_descriptor MyNodeDataID;
typedef Graph::edge_descriptor MyEdgeDataID;
typedef graph_traits < Graph >::vertex_iterator VertexIterator;
typedef graph_traits < Graph >::edge_iterator EdgeIterator;
typedef graph_traits < Graph >::adjacency_iterator AdjacencyIterator;
typedef property_map < Graph, vertex_index_t >::type IndexMap;

Graph g;
const IndexMap index = get(vertex_index, g);

/* Puis après avoir ajouté des vertex et edges, je peux accéder par exemple à la liste des vertex comme suite: */
pair<VertexIterator, VertexIterator> vi;
for(vi = vertices(g); vi.first != vi.second; ++vi.first)
{
   cout << "vertex: " << index[*vi.first] << endl;
   // or: cout << "vertex: " << *vi.first << endl;
}

但我通常需要在图表中添加/删除边和顶点 . 所以我想使用setS或listS作为VertexList而不是vecS,因为当我们删除其中一个时,使用vecS索引会失效!问题是,如果我将VertexList定义为setS或listS,我无法浏览顶点/边的列表并像以前那样访问那些描述符!

简而言之,我的问题是:由于使用listS或setS作为顶点容器的adjacency_list不会自动提供此vertex_id属性,如何将其添加到上面的代码中?

2 回答

  • 0

    目前,您只需要提供关联属性映射 .

    <...>
    typedef Graph::vertex_descriptor NodeID;
    
    typedef map<NodeID, size_t> IndexMap;
    IndexMap mapIndex;
    associative_property_map<IndexMap> propmapIndex(mapIndex);
    <...>
    
    // indexing all vertices
    int i=0;
    BGL_FORALL_VERTICES(v, g, Graph)
    {
       put(propmapIndex, v, i++);
    }
    
  • 0

    但我通常需要在图表中添加/删除边和顶点 .

    使用vecS,setS and listS可以删除顶点和边 . 只需使用顶点\边描述符调用remove_vertex \ remove_edge . 在以上所有容器中,删除\添加顶点\ edge将使迭代器无效 . 这意味着在修改图形之后,您将不得不再次调用顶点(g) . 在大多数容器中,修改容器会使迭代器无效 . 在listS中,添加顶点可能不会使迭代器无效,但这是特定于实现的,不应该依赖它 .

    您可以将vertex_id属性添加到图形中,从而允许您随时访问顶点描述符 .

相关问题