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

后端 未结 7 1951
天命终不由人
天命终不由人 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 10:56

    It's a habit for generic programming; for example, you can easiely use < with indices, but you cannot use that with all iterator types. A list iterator cannot efficiently implement < - however, != can be implemented for even the simplest of iterator types. Therefore, it is a good habit to always use the most generic comparison - it makes your code more resilient to change.

提交回复
热议问题