迭代范围,然后“再一次”

发布于 2024-11-30 02:59:04 字数 631 浏览 3 评论 0原文

在我当前正在实现的算法中,有这一行(其中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2024-12-07 02:59:04

我认为您使用的解决方案是好的(只要 Do stuff 代码分解得很好)。

但是,如果您经常遇到此类问题,可以查看 Boost.Range ,一个用于操作值范围而不是迭代器的库。在这里,您可以使用 join 函数 来获取两个范围的并集(boost::in_edgesu 的结果)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文