Iterate over range, and "one more"
In the algorithm I'm currently implementing, there is this line (where u
is a vertex in a graph, and Pred(u)
are all vertices having edges pointing at u
):
for all s ∈ Pred(u) ∪ {u}
The Pred(u)
part I translate into boost::graph code like this:
boost::graph_trai开发者_高级运维ts<Graph>::in_edge_iterator in_begin, in_end;
boost::tie(in_begin, in_end) = boost::in_edges(u, G);
for(boost::graph_traits<Graph>::in_edge_iterator i = in_begin; i != in_end; ++i) {
// Do stuff
}
For now, I'm doing the Do stuff
stuff outside of the loop for u
explicitly, but I'd like to do it in the for
loop. Is there some trick to create the iterators as if u
was returned from boost::in_edges
?
I think the solution you are using is alright (as long as the Do stuff
code is well factorized).
However, if you often face such issues, you can have a look at Boost.Range, a library for manipulating ranges of values instead of iterators. Here, you could use the join function to obtain the union of your two ranges (the result of boost::in_edges
and u
).
精彩评论