对于可能在结束之前中断的迭代使用什么循环?

发布于 2024-09-24 07:19:46 字数 508 浏览 1 评论 0原文

我有一系列内存需要解析。如果我在结束之前找到某个字节序列,我会中断迭代。我想知道我应该在这里更喜欢哪个循环:

while(i < end && !sequenceFound ) {
    // parse
    i++;
}

或者

for( i; i < end && !sequenceFound; i++ ) {
    // parse
}

这是在派生自实现环形缓冲区的类的类的方法中使用的。超类提供了iend。我的问题是,您认为对于不熟悉代码的人来说哪一个更容易理解(更好地表达意图)?

编辑 事实上,我发现该序列是进一步解析流所必需的。我可以使用 break 并设置 sequenceFound = true,但这会是多余的,还是我在这里太严格了?

I have a range of memory to parse. If I find a certain sequence of bytes before the end, I interrupt the iteration. I wonder which loop I should prefer here:

while(i < end && !sequenceFound ) {
    // parse
    i++;
}

Or

for( i; i < end && !sequenceFound; i++ ) {
    // parse
}

This is used in a method of a class that derives from a class that implements a ring buffer. The superclass provides i and end. My question is, which one do you think is easier to understand (expresses the intend better) for someone unfamiliar with the code?

Edit The fact that I found the sequence is needed for the further parsing of the stream. I could use break and set sequenceFound = true, but that would be redundant, or am I being to strict here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

深居我梦 2024-10-01 07:19:46

为什么不在需要“中断”循环时使用 break; 呢?这似乎是最能惯用地表达您的意图的语言功能。这通常意味着您可以不需要额外的布尔状态跟踪变量。

如果您需要知道迭代是否提前终止,可以使用条件 i != end。 无论哪种方式,使用最清晰的控制方法似乎都是最好的,并且 break;无论你是否保持“提前退出”变量,在我看来,你想要打破的时间点似乎是最清楚的。继续循环并测试您刚刚保证会失败的条件似乎是多余的。

Why not just use break; at the point that the need to "interrupt" the loop is encountered. This seems like the language feature that most idiomatically expresses your intent. It usually means that you can do without the extra boolean state tracking variable.

If you need to know whether the iteration terminated early you can use the condidition i != end. Either way, using the control method that is clearest would seem best and break; at the point at which you want to break seems clearest to me, whether or not you maintain an "early exit" variable. It seems redundant to carry on round the loop and test a condition that you've only just guaranteed will fail.

小帐篷 2024-10-01 07:19:46

更喜欢算法而不是手写循环。

#include <algorithm>

auto it = std::find(begin, end, predicate);
if (it == end)
    :-(
else
    :-)

Prefer algorithms to hand-written loops.

#include <algorithm>

auto it = std::find(begin, end, predicate);
if (it == end)
    :-(
else
    :-)
香橙ぽ 2024-10-01 07:19:46

对于具有明确限制的迭代,我通常会使用 for 循环。当迭代次数没有上限时,应使用 while 循环。

For an iteration that has an explicit limit, I would normally use a for loop. While loops should be used when the iteration has no upper limit.

一袭白衣梦中忆 2024-10-01 07:19:46

对于具有循环变量和结束的循环(即,对于通过检查 i != endi < end 在一定范围内运行的所有循环),我喜欢 for 方法,因为这或多或少是 for 的规范用法。

如果您可能会过早离开循环,请break

for(; i != end; ++i) {
    // parse
    if (sequenceFound)
        break;
}

For loops with a loop variable and an end (i.e. for all loops that run over a range by checking i != end or i < end) I favour the for approach since this is more or less the canonical usage of for.

If you may leave the loop prematurely, break:

for(; i != end; ++i) {
    // parse
    if (sequenceFound)
        break;
}
挖鼻大婶 2024-10-01 07:19:46

我更喜欢这里的 while 因为这样读起来更好。不过可能是个人喜好。 until 在某些语言中可用也很好。

I favour while here as that reads better. Might be a personal preference though. until which is available in some languages would be good as well.

血之狂魔 2024-10-01 07:19:46

第二个版本更容易理解并允许使用 continue 运算符。

The second version is better for understanding and allows to use continue operator.

情痴 2024-10-01 07:19:46

如果我在每次迭代之前要检查多个条件,我通常会使用 while 循环。

If I have more than one condition to check before each iteration I usually use a while loop.

┼── 2024-10-01 07:19:46

同意 Charles Bailey 的观点,因为一旦你中断迭代,你就会立即脱离最直接的循环结构,而不必重新评估条件变量。此外,您不需要额外的条件变量,从而减少(稍微减少)代码块运行所需的内存。

Agree with Charles Bailey as once you break in the iteration, you immediatley break out of the most immediate looping structure, without having to re-evaluate a condition variable. Also, you do not need the additional condition variable, thus reducing (slightly that is) memory required for the block of code to run.

站稳脚跟 2024-10-01 07:19:46

我更喜欢内部带有 breakfor

原因是您可以避免必须检查“中断”条件的丑陋构造,例如:

for(; i != end; ++i) {

    // do stuff

    if (sequenceFound) break;

    // you don't need an 'if(!sequenceFound)' around this block of code
    // do more stuff
}

但是使用 while 版本或带有内置条件的 for ,当然取决于您正在做什么,您可以'不要那么轻易地避免这个条件。

I prefer the for with the break internally.

The reason being that you can avoid ugly constructs that have to check your 'break' condition, e.g.:

for(; i != end; ++i) {

    // do stuff

    if (sequenceFound) break;

    // you don't need an 'if(!sequenceFound)' around this block of code
    // do more stuff
}

But with either the while version or the for with the condition built-in, depending of course on what you're doing, you can't so easily avoid that conditional.

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