What's the best way to do a backwards loop in C/C#/C++?

后端 未结 14 2659
终归单人心
终归单人心 2020-11-28 01:39

I need to move backwards through an array, so I have code like this:

for (int i = myArray.Length - 1; i >= 0; i--)
{
    // Do something
    myArray[i] =          


        
14条回答
  •  無奈伤痛
    2020-11-28 02:05

    I'm going to try answering my own question here, but I don't really like this, either:

    for (int i = 0; i < myArray.Length; i++)
    {
        int iBackwards = myArray.Length - 1 - i; // ugh
        myArray[iBackwards] = 666;
    }
    

提交回复
热议问题