[Solved] Why does this run into infinite loop? [closed]


The problem is in comparing to an unsigned number after an underflow.

The size of the vector is 1. You subtracr 2, and get -1 mathematically. In unsigned math, however, you get a very large number, so your loop continues for much longer than you expected.

To avoid situations like this, replace subtraction with addition:

i+2 <= v.size()

1

solved Why does this run into infinite loop? [closed]