Is there a technical reason to use > (<) instead of != when incrementing by 1 in a 'for' loop?

后端 未结 21 2066
小蘑菇
小蘑菇 2020-12-23 19:48

I almost never see a for loop like this:

for (int i = 0; 5 != i; ++i)
{}

Is there a technical reason to use >

21条回答
  •  抹茶落季
    2020-12-23 20:50

    Iterators are an important case when you most often use the != notation:

    for(auto it = vector.begin(); it != vector.end(); ++it) {
     // do stuff
    }
    

    Granted: in practice I would write the same relying on a range-for:

    for(auto & item : vector) {
     // do stuff
    }
    

    but the point remains: one normally compares iterators using == or !=.

提交回复
热议问题