Why do c++ programmers use != instead of <

后端 未结 7 1950
天命终不由人
天命终不由人 2020-12-16 10:28

In c++ primer, pg 95 the author says that c++ programmers tend to use != in preference of < when writing loops.

for (vector::size_type i = 0;          


        
7条回答
  •  天涯浪人
    2020-12-16 11:10

    Think of the case when one have to increment by lets say 3 instead of 1.

    for (vector::size_type i = 0; i != 10; i+=3)
    

    This will run forever since it will skip 10 and go to 12 instead and increment forever.

    for (vector::size_type i = 0; i < 10; i+=3) 
    

    This will work fine in this case too. So != is not always a good choice.

提交回复
热议问题