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

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

    If you write !=, then you can reverse the loop iteration with minimal change.

    Suppose you first write:

    for ( int i = m; i != n ; i++ )
    

    Later you reverse it:

    for ( int i = n ; i != m ; i-- )
    

    Not so appealing, but still it requires less analysis than "<" and ">".

提交回复
热议问题