[opensource-dev] iterators, finding extrema with for-loops (was: Review Request: Squared all dist_vec() based comparisons and other dist_vec() operations where sensible.)
sllists at boroon.dasgupta.ch
Fri Mar 11 11:34:11 PST 2011
On 03/11/2011 05:43 PM, Ricky wrote:
> As to the variables that are initialized with high numbers, there has
> to be a better way: if these were standard for-loops, I would just
> initialize the first value with the first item in the list, and then
> start the loop at the second value, if any. However, these are a
> iterator style I am not yet familiar enough with to bend that far.
> Any suggestions?
Don't let iterators scare you. They are just objects of special classes.
That these classes are sometimes created through fancy templates doesn't
have to bother you. The only thing you have to know, is that they
override the dereference operators and (at least) one iteration operator
(increment or decrement). So you can treat them almost like pointers
into an array, even if they point into another data structure (e.g. a
* You can dereference them to get to the value they're "pointing" to.
* You can iterate them to have them point to the "next" element of
the data structure.
Both can be done outside of loops.
More info e.g. at http://www.cppreference.com/wiki/iterator/start
However, for the loops at hand, some more computation beyond the raw
data access happens, which would have to be duplicated outside the loop
if you want to initialize the variable with the result of the first
iteration. This is probably not worth the trouble (What does a single
assignment 'cost', anyway?) and would make the code harder to
understand. Also, you'd need checks whether the iterated data structures
aren't empty etc.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the opensource-dev