如何在 C++ 中放置两个增量语句 “为了” 环形?
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尽量不要这样做!
来自 http://www.research.att.com/~bs/JSF-AV-rules.pdf:
Try not to do it!
From http://www.research.att.com/~bs/JSF-AV-rules.pdf:
尝试这个
Try this
我来这里是为了提醒自己如何将第二个索引编码到 FOR 循环的增量子句中,我知道这主要可以通过在我合并到另一个用 C++ 编写的项目中的示例中观察来完成。
今天,我使用 C# 工作,但我确信它在这方面会遵循相同的规则,因为 FOR 语句是所有编程中最古老的控制结构之一。 值得庆幸的是,我最近花了几天时间精确记录了我的一个旧 C 程序中 FOR 循环的行为,我很快意识到这些研究提供了适用于当今 C# 问题的经验教训,特别是第二个索引变量的行为。
对于粗心的人来说,以下是我的观察总结。 通过仔细观察 Locals 窗口中的变量,我今天所看到的一切都证实了我的预期:C# FOR 语句的行为与 C 或 C++ 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.
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.
使用数学。 如果这两个运算在数学上取决于循环迭代,为什么不进行数学计算呢?
或者,更具体地参考OP的示例:
特别是如果您按值传递给函数,那么您应该得到完全符合您想要的功能的东西。
Use Maths. If the two operations mathematically depend on the loop iteration, why not do the math?
Or, more specifically referring to the OP's example:
Especially if you're passing into a function by value, then you should get something that does exactly what you want.
我同意斯奎拉特的观点。 增加两个变量很容易出现错误,特别是如果您只测试其中之一。
这是执行此操作的一种可读方法:
For
循环适用于循环在一个递增/递减变量上运行的情况。 对于任何其他变量,请在循环中更改它。如果您需要将
j
绑定到i
,为什么不保持原始变量不变并添加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:
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 toi
, why not leave the original variable as is and addi
?If your logic is more complex (for example, you need to actually monitor more than one variable), I'd use a
while
loop.常见的习惯用法是使用 逗号运算符 来计算两个操作数,并返回第二个操作数。 因此:
但这真的是一个逗号运算符吗?
现在写完后,一位评论者表示这实际上是 for 语句中的一些特殊语法糖,而不是逗号运算符。 我在 GCC 中进行了如下检查:
我期望 x 获取 a 的原始值,因此它应该为 x 显示 5,6,7..。 我得到的是这个
但是,如果我将表达式括起来以强制解析器真正看到逗号运算符,我得到这个
最初我认为这表明它根本不表现为逗号运算符,但事实证明,这只是一个优先级问题 - 逗号运算符具有 最低可能优先级,因此表达式 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:
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:
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
However, if I bracketed the expression to force the parser into really seeing a comma operator, I get this
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!