I see somewhere it mentions:
for ( itr = files.begin(); itr < files.end(); ++itr ) // WRONG
for ( itr = files.begin(); itr != files.end(); ++itr ) // ok
Why is the first expression wrong? I always used the first expression, and didn't have any problems.
Ordering comparisons such as <
, >
, <=
, >=
will work for random-access iterators, but many other iterators (such as bidirectional iterators on linked lists) only support equality testing (==
and !=
). By using !=
you can later replace the container without needing to change as much code, and this is especially important for template code which needs to work with many different container types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With