Never seen before C++ for loop

后端 未结 12 2096
独厮守ぢ
独厮守ぢ 2020-12-07 10:58

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;

It gives no

12条回答
  •  春和景丽
    2020-12-07 11:34

    The condition is the result of u--, which is the value of u before it was decremented.

    In C and C++, an int is convertible to bool by implicitly doing a != 0 comparison (0 is false, everything else is true).

    b.back() is the last element in a container, which is b[b.size() - 1], when size() != 0.

提交回复
热议问题