Since tmp.size()
is 1
, subtracting 3 from it produces a negative value. That is, subtraction would produce a negative value if it weren’t for tmp.size()
being size_t
, which is unsigned.
When an unsigned is about to go negative on subtraction, it “wraps around” to a very large value. That is why your loop fails to stop. It also explains the “signed to unsigned comparison” warning produced by the compiler*, which should have put you on high alert.
To avoid this problem, move subtraction to the other side, and make it addition:
for (size_t i = 0 ; i+3 <= tmp.size() ; i += 2) {
...
}
* If you did not get that warning, change the settings on your compiler to warn you about everything. This saves a lot of time spent in debugging many common problems that are easily detectable by compilers.
0
solved Why does the following loop execute