Why is it better to use \'!=\" than \'<\' in a vector loop? I just can\'t see the difference.
I'll assume that you mean the use of !=vector.end()
rather than
One reason to prefer the end() version is that what you are really trying to do is to check whether you reached the end.
The idiom for doing this sort of checks for arrays in C was to check the index compared to the size, but that is a "legacy" behavior. In was then spread in the world of Java (as <array.length
) until such time that people realized that arrays are evil and that Java finally supports generics. In C++, however, there is no reason to use this idiom when you have a good collection class such as vector to work with. It's more readable, and more maintainable. For example, it makes it easier to switch to other collections if necessary, as you are still talking about the end.