Never seen before C++ for loop

后端 未结 12 2124
独厮守ぢ
独厮守ぢ 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:31

    This will be the C# form of your loop.

    // back fetches the last element of vector in c++.
    for (u = b.size(), v = b.back(); (u--) != 0; v = p[v]) 
    {      
      b[u] = v;      
    }
    

    Just replace equivalent for size() and back().

    What it does is reverses the list and stores in an array. But in C# we directly have system defined function for this. So you don't need to write this loop also.

    b = b.Reverse().ToArray();
    

提交回复
热议问题