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

后端 未结 7 1977
天命终不由人
天命终不由人 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:51

    When using some kinds of STL iterators (those that aren't random access), you must use !=:

    for (map::iterator i = a.begin(); i != a.end(); ++i) ...
    

    However, I don't see any reason to prefer != for well-ordered scalar types as in your example. I would usually prefer < for scalar types and != for all iterator types.

提交回复
热议问题