Unsigned int reverse iteration with for loops

后端 未结 14 1127
离开以前
离开以前 2020-12-07 17:00

I want the iterator variable in a for loop to reverse iterate to 0 as an unsigned int, and I cannot think of a similar comparison to i > -1, as

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 17:02

    I am new to c++, my answer might be realy stupid but, i do this for this kind of reverse loops;

    size_t count = 3;
    size_t newCount = 0;
    if (count > 0)
        for (size_t i = count - 1; i >= newCount; i--)
        {
            //do your work here
    
            //and at the end
            if (i == 0)
                break;
        }
    

    and it works. since "i--" part at the loop executed at the next step, break should work allright. what do you think is this way safe ?

提交回复
热议问题