如何在 C++ 中放置两个增量语句 “为了” 环形?

发布于 2024-07-30 00:01:04 字数 152 浏览 5 评论 0原文

我想在 for 循环条件中增加两个变量而不是一个。

比如:

for (int i = 0; i != 5; ++i and ++j) 
    do_something(i, j);

这个的语法是什么?

I would like to increment two variables in a for-loop condition instead of one.

So something like:

for (int i = 0; i != 5; ++i and ++j) 
    do_something(i, j);

What is the syntax for this?

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

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

发布评论

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

评论(8

后来的我们 2024-08-06 00:01:05

尽量不要这样做!

来自 http://www.research.att.com/~bs/JSF-AV-rules.pdf

AV 规则 199
for 循环中的增量表达式除了更改单个值之外不会执行任何操作
将循环参数设置为循环的下一个值。

理由:可读性。

Try not to do it!

From http://www.research.att.com/~bs/JSF-AV-rules.pdf:

AV Rule 199
The increment expression in a for loop will perform no action other than to change a single
loop parameter to the next value for the loop.

Rationale: Readability.

已下线请稍等 2024-08-06 00:01:05

尝试这个

for(int i = 0; i != 5; ++i, ++j)
    do_something(i,j);

Try this

for(int i = 0; i != 5; ++i, ++j)
    do_something(i,j);
我早已燃尽 2024-08-06 00:01:05
for (int i = 0; i != 5; ++i, ++j) 
    do_something(i, j);
for (int i = 0; i != 5; ++i, ++j) 
    do_something(i, j);
埋情葬爱 2024-08-06 00:01:05

我来这里是为了提醒自己如何将第二个索引编码到 FOR 循环的增量子句中,我知道这主要可以通过在我合并到另一个用 C++ 编写的项目中的示例中观察来完成。

今天,我使用 C# 工作,但我确信它在这方面会遵循相同的规则,因为 FOR 语句是所有编程中最古老的控制结构之一。 值得庆幸的是,我最近花了几天时间精确记录了我的一个旧 C 程序中 FOR 循环的行为,我很快意识到这些研究提供了适用于当今 C# 问题的经验教训,特别是第二个索引变量的行为。

对于粗心的人来说,以下是我的观察总结。 通过仔细观察 Locals 窗口中的变量,我今天所看到的一切都证实了我的预期:C# FOR 语句的行为与 C 或 C++ FOR 语句完全相同。

  1. 第一次执行 FOR 循环时,将跳过增量子句(三个子句中的第三个)。 在 Visual C 和 C++ 中,增量是在实现循环的块中间生成为三个机器指令,因此初始阶段仅运行一次初始化代码,然后跳过增量块来执行终止测试。 这实现了 FOR 循环执行零次或多次的功能,具体取决于其索引和限制变量的状态。
  2. 如果执行循环体,则其最后一条语句将跳转到第一次迭代跳过的三个增量指令中的第一个指令。 在这些执行之后,控制自然落入实现中间子句的限制测试代码中。 该测试的结果确定 FOR 循环的主体是否执行,或者控制是否转移到经过其范围底部的跳转的下一条指令。
  3. 由于控制从 FOR 循环块的底部转移到增量块,因此索引变量在执行测试之前递增。 这种行为不仅解释了为什么您必须按照您学到的方式编码限制子句,而且它会影响您通过逗号运算符添加的任何辅助增量,因为它成为第三个子句的一部分。 因此,它在第一次迭代时没有改变,但在最后一次迭代时改变,它从不执行主体。

如果循环结束时任一索引变量仍在范围内,则在真正的索引变量的情况下,它们的值将比停止循环的阈值高。 同样,例如,如果在进入循环之前将第二个变量初始化为零,则其最后的值将是迭代计数,假设它是增量 (++),而不是减量,并且其中没有任何内容循环体改变了它的值。

I came here to remind myself how to code a second index into the increment clause of a FOR loop, which I knew could be done mainly from observing it in a sample that I incorporated into another project, that written in C++.

Today, I am working in C#, but I felt sure that it would obey the same rules in this regard, since the FOR statement is one of the oldest control structures in all of programming. Thankfully, I had recently spent several days precisely documenting the behavior of a FOR loop in one of my older C programs, and I quickly realized that those studies held lessons that applied to today's C# problem, in particular to the behavior of the second index variable.

For the unwary, following is a summary of my observations. Everything I saw happening today, by carefully observing variables in the Locals window, confirmed my expectation that a C# FOR statement behaves exactly like a C or C++ FOR statement.

  1. The first time a FOR loop executes, the increment clause (the 3rd of its three) is skipped. In Visual C and C++, the increment is generated as three machine instructions in the middle of the block that implements the loop, so that the initial pass runs the initialization code once only, then jumps over the increment block to execute the termination test. This implements the feature that a FOR loop executes zero or more times, depending on the state of its index and limit variables.
  2. If the body of the loop executes, its last statement is a jump to the first of the three increment instructions that were skipped by the first iteration. After these execute, control falls naturally into the limit test code that implements the middle clause. The outcome of that test determines whether the body of the FOR loop executes, or whether control transfers to the next instruction past the jump at the bottom of its scope.
  3. Since control transfers from the bottom of the FOR loop block to the increment block, the index variable is incremented before the test is executed. Not only does this behavior explain why you must code your limit clauses the way you learned, but it affects any secondary increment that you add, via the comma operator, because it becomes part of the third clause. Hence, it is not changed on the first iteration, but it is on the last iteration, which never executes the body.

If either of your index variables remains in scope when the loop ends, their value will be one higher than the threshold that stops the loop, in the case of the true index variable. Likewise, if, for example, the second variable is initialized to zero before the loop is entered, its value at the end will be the iteration count, assuming that it is an increment (++), not a decrement, and that nothing in the body of the loop changes its value.

乞讨 2024-08-06 00:01:05
int main(){
    int i=0;
    int a=0;
    for(i;i<5;i++,a++){
        printf("%d %d\n",a,i);
    } 
}
int main(){
    int i=0;
    int a=0;
    for(i;i<5;i++,a++){
        printf("%d %d\n",a,i);
    } 
}
毁梦 2024-08-06 00:01:05

使用数学。 如果这两个运算在数学上取决于循环迭代,为什么不进行数学计算呢?

int i, j;//That have some meaningful values in them?
for( int counter = 0; counter < count_max; ++counter )
    do_something (counter+i, counter+j);

或者,更具体地参考OP的示例:

for(int i = 0; i != 5; ++i)
    do_something(i, j+i);

特别是如果您按值传递给函数,那么您应该得到完全符合您想要的功能的东西。

Use Maths. If the two operations mathematically depend on the loop iteration, why not do the math?

int i, j;//That have some meaningful values in them?
for( int counter = 0; counter < count_max; ++counter )
    do_something (counter+i, counter+j);

Or, more specifically referring to the OP's example:

for(int i = 0; i != 5; ++i)
    do_something(i, j+i);

Especially if you're passing into a function by value, then you should get something that does exactly what you want.

伴我心暖 2024-08-06 00:01:05

我同意斯奎拉特的观点。 增加两个变量很容易出现错误,特别是如果您只测试其中之一。

这是执行此操作的一种可读方法:

int j = 0;
for(int i = 0; i < 5; ++i) {
    do_something(i, j);
    ++j;
}

For 循环适用于循环在一个递增/递减变量上运行的情况。 对于任何其他变量,请在循环中更改它。

如果您需要将 j 绑定到 i,为什么不保持原始变量不变并添加 i 呢?

for(int i = 0; i < 5; ++i) {
    do_something(i,a+i);
}

如果您的逻辑更复杂(例如,您需要实际监视多个变量),我会使用 while 循环。

I agree with squelart. Incrementing two variables is bug prone, especially if you only test for one of them.

This is the readable way to do this:

int j = 0;
for(int i = 0; i < 5; ++i) {
    do_something(i, j);
    ++j;
}

For loops are meant for cases where your loop runs on one increasing/decreasing variable. For any other variable, change it in the loop.

If you need j to be tied to i, why not leave the original variable as is and add i?

for(int i = 0; i < 5; ++i) {
    do_something(i,a+i);
}

If your logic is more complex (for example, you need to actually monitor more than one variable), I'd use a while loop.

初懵 2024-08-06 00:01:04

常见的习惯用法是使用 逗号运算符 来计算两个操作数,并返回第二个操作数。 因此:

for(int i = 0; i != 5; ++i,++j) 
    do_something(i,j);

但这真的是一个逗号运算符吗?

现在写完后,一位评论者表示这实际上是 for 语句中的一些特殊语法糖,而不是逗号运算符。 我在 GCC 中进行了如下检查:

int i=0;
int a=5;
int x=0;

for(i; i<5; x=i++,a++){
    printf("i=%d a=%d x=%d\n",i,a,x);
}

我期望 x 获取 a 的原始值,因此它应该为 x 显示 5,6,7..。 我得到的是这个

i=0 a=5 x=0
i=1 a=6 x=0
i=2 a=7 x=1
i=3 a=8 x=2
i=4 a=9 x=3

但是,如果我将表达式括起来以强制解析器真正看到逗号运算符,我得到这个

int main(){
    int i=0;
    int a=5;
    int x=0;

    for(i=0; i<5; x=(i++,a++)){
        printf("i=%d a=%d x=%d\n",i,a,x);
    }
}

i=0 a=5 x=0
i=1 a=6 x=5
i=2 a=7 x=6
i=3 a=8 x=7
i=4 a=9 x=8

最初我认为这表明它根本不表现为逗号运算符,但事实证明,这只是一个优先级问题 - 逗号运算符具有 最低可能优先级,因此表达式 x= i++,a++ 被有效地解析为 (x=i++),a++

感谢所有的评论,这是一次有趣的学习经历,我已经使用 C 多年了!

A common idiom is to use the comma operator which evaluates both operands, and returns the second operand. Thus:

for(int i = 0; i != 5; ++i,++j) 
    do_something(i,j);

But is it really a comma operator?

Now having wrote that, a commenter suggested it was actually some special syntactic sugar in the for statement, and not a comma operator at all. I checked that in GCC as follows:

int i=0;
int a=5;
int x=0;

for(i; i<5; x=i++,a++){
    printf("i=%d a=%d x=%d\n",i,a,x);
}

I was expecting x to pick up the original value of a, so it should have displayed 5,6,7.. for x. What I got was this

i=0 a=5 x=0
i=1 a=6 x=0
i=2 a=7 x=1
i=3 a=8 x=2
i=4 a=9 x=3

However, if I bracketed the expression to force the parser into really seeing a comma operator, I get this

int main(){
    int i=0;
    int a=5;
    int x=0;

    for(i=0; i<5; x=(i++,a++)){
        printf("i=%d a=%d x=%d\n",i,a,x);
    }
}

i=0 a=5 x=0
i=1 a=6 x=5
i=2 a=7 x=6
i=3 a=8 x=7
i=4 a=9 x=8

Initially I thought that this showed it wasn't behaving as a comma operator at all, but as it turns out, this is simply a precedence issue - the comma operator has the lowest possible precedence, so the expression x=i++,a++ is effectively parsed as (x=i++),a++

Thanks for all the comments, it was an interesting learning experience, and I've been using C for many years!

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