Why is it better to use '!=" than '<' in a vector loop? (C++)

后端 未结 7 1388
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 04:53

Why is it better to use \'!=\" than \'<\' in a vector loop? I just can\'t see the difference.

7条回答
  •  抹茶落季
    2021-01-02 05:31

    It could be that the programmer remembered a defensive coding trick: In case something goes haywire during the loop's execution, and the index/pointer/iterator goes way past the end, the loop will quietly stop upon checking '<' leaving the bug unnoticed. With != there's likely to be chaos and a crash as the loop continues with bad data, allowing an earlier fix.

提交回复
热议问题