|
|
|
|
|
by duneroadrunner
3536 days ago
|
|
"Idiomatic" doesn't necessarily mean better. I think objectively it's hard to argue that "item != market.end()" is superior to "iter.hasNext()". The latter accurately reflects the programmer's intent, while the former specifies an unnecessarily specific (and poor) implementation of the intent. First of all, using something like "market.cend() != const_iter" instead is arguably better practice (imagine you unintentionally omit the "!"). But programmers shouldn't need to consider whether the iterator is const or not when they just want to know if the loop is done. Also, consider the case where the vector is being modified (items inserted or deleted) inside the loop. It might be problematic either way, but "item != market.end()" is particularly bad in that situation. Shameless plug:
http://duneroadrunner.github.io/SaferCPlusPlus/#msevector |
|
Until you realize the purpose of the weird syntax in the former.
Namely: you can write algorithms where an iterator is a drop-in replacement for a simple loop through all items in an array via pointer arithmetic, if you write it to take a start and an end iterator as templates.
Plus said algorithm can take sub-ranges instead of requiring it to loop through all items.
I thought c++ iterators were very strange for many years until I understood this and other rationales.