Infinite loop when using size_t in a count down for loop

后端 未结 7 1235
走了就别回头了
走了就别回头了 2021-01-13 07:53

So I\'m using size_t instead of int in any indexing for loop to prevent negative indices. But when counting down, this leads to an overflow:

<
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-13 08:13

    The idiomatic, though not to everyone's taste way, is to use the slide operator:

    for (size_t i = 10 + 1; i--> 0; )
    

    It isn't really an operator but that's what it has become known as over the years.

提交回复
热议问题