Unsigned int reverse iteration with for loops

后端 未结 14 1126
离开以前
离开以前 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 17:22

    Are you really iterating down from some number greater than std::numeric_limits::max()? If not, I would actually suggest just using a normal int as your loop variable and static_cast it to unsigned in the places in your code that expect it to be unsigned. This way you can use the intuitive >= 0 or > -1 condition and in general I would expect it to be more readable than any of the unsigned alternatives.

    The static_cast would just be to tell the compiler how to operate on the variable and have no performance implications at all.

提交回复
热议问题