C ++ for循环之前从未见过 [英] Never seen before C++ for loop
问题描述
我是一个C ++算法转换为C#。
我碰到这个来抓循环:
I was converting a C++ algorithm to C#. I came across this for loop:
for (u = b.size(), v = b.back(); u--; v = p[v])
b[u] = v;
这使在C ++中没有错误,但在C#中不(不能转换INT为bool)。
我真的想不通这个for循环,这里是条件?
It gives no error in C++, but it does in C# (cannot convert int to bool). I really can't figure out this for loop, where is the condition?
有人能解释一下吗?
的 PS。只是为了检查,在清单上,以适应VECTOR
b.back()
相当于
B〔b.Count-1]
?的
推荐答案
的为
循环的条件是在中间 - 这两个分号;
The condition of the for
loop is in the middle - between the two semicolons ;
.
在C ++中它是确定把几乎所有的前pression作为条件:任何计算结果为零手段假
;非零的手段真正
。
In C++ it is OK to put almost any expression as a condition: anything that evaluates to zero means false
; non-zero means true
.
在你的情况,条件是û -
:当你转换为C#中,只需添加 = 0
:
In your case, the condition is u--
: when you convert to C#, simply add != 0
:
for (u = b.size(), v = b.back(); u-- != 0; v = p[v])
b[u] = v; // ^^^^ HERE
这篇关于C ++ for循环之前从未见过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!