// display vector elements using const_iterator
for ( constIterator = integers.begin();
constIterator != integers.end(); ++constIterator )
cout << *constIterator << ' ';
Can we use constIterator < integers.end()
?
Thank you
operator<
is only defined for random access iterators. These are provided, for example, by std::vector
and std::string
, containers that, in essence, store their data in contiguous storage, where iterators are usually little more than wrapped pointers. Iterators provided by, e.g., std::list
are only bidirectional iterators, which only provide comparison for equality.
Traditionally, it's seen as defensive programming to use <
instead of !=
. In case of errors (for example, someone changes ++i
to i+=2
) the loop will terminate even though the exact end value is never reached. However, another view at this is that it might mask an error, while the loop running endlessly or causing a crash would make the error apparent.
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