Boost Graph: iterating over internal vertex property
Hi, My graph is defined like this: typedef adjacency_list<vecS, vecS, directedS, Foo*> Graph; The vertex indices can be iterated over like this: typedef property_map<Graph, vertex_index_t>::type IndexMap; IndexMap index = get(vertex_index, g); typedef graph_traits<Graph>::vertex_iterator vertex_iter; std::pair<vertex_iter, vertex_iter> vp; for (vp = vertices(g); vp.first != vp.second; ++vp.first) { std::cout << index[*vp.first] << " "; } (This is straight out of the "Quick Tour" document.) But what if I want to iterate over the Foo pointers in my graph? I suppose one way is this: for (int i = 0; i < num_vertices(g); i++) { Foo *foo = g[i]; } But I assume this wouldn't scale if I were to use listS instead vecS. Also, I'd want to use a proper vertex_iterator anyway, so I can grab all the properties I want in one loop. But how can I access my Foo pointer from the vertex_iterator? I've looked though the docs and sample code but I just can't figure it out. Thanks for any tips, Trevor
But what if I want to iterate over the Foo pointers in my graph? I suppose one way is this:
for (int i = 0; i < num_vertices(g); i++) { Foo *foo = g[i]; }
Yup. But I assume this wouldn't scale if I were to use listS instead vecS. Also,
I'd want to use a proper vertex_iterator anyway, so I can grab all the properties I want in one loop. But how can I access my Foo pointer from the vertex_iterator? I've looked though the docs and sample code but I just can't figure it out.
Pretty much the same way: typename graph_traits<G>::vertex_iterator i, end; for(tie(i, end) = vertices(g); i != end; ++ii) { Foo* f = g[*i]; } Dereferencing a vertex iterator returns a vertex descriptor. The same is true for edge iterators and edge descriptors, if it comes up. Andrew Sutton andrew.n.sutton@gmail.com
participants (2)
-
Andrew Sutton
-
Trevor Harmon