gpt4 book ai didi

c++ - adjacency_list 与 VertexList 不同于 vecS

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:00:43 25 4
gpt4 key购买 nike

我有两个包含一些字段的结构: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 属性,我如何将其添加到上面的代码中?

最佳答案

目前,您只需要提供一个关联的属性映射。

<...>
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++);
}

关于c++ - adjacency_list 与 VertexList 不同于 vecS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7768158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com