迭代范围,然后“再一次”
在我当前正在实现的算法中,有这一行(其中 u
是图中的顶点,而 Pred(u)
是具有指向 < 的边的所有顶点code>u):
for all s ∈ Pred(u) ∪ {u}
我将 Pred(u)
部分翻译成 boost::graph 代码,如下所示:
boost::graph_traits<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
}
现在,我正在做 Do stuff
显式地在 for u
循环之外添加内容,但我想在 for 循环中执行此操作。是否有一些技巧可以创建迭代器,就好像 u
是从 boost::in_edges
返回的一样?
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_traits<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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您使用的解决方案是好的(只要
Do stuff
代码分解得很好)。但是,如果您经常遇到此类问题,可以查看 Boost.Range ,一个用于操作值范围而不是迭代器的库。在这里,您可以使用 join 函数 来获取两个范围的并集(
boost::in_edges
和u
的结果)。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
andu
).