Iterators Must Go

Walter Bright newshound1 at digitalmars.com
Fri May 8 08:57:41 PDT 2009


Steven Schveighoffer wrote:
> You still have not addressed the usage of iterators as general data 
> structure pointers.  As far as I can tell, ranges do not implement this.
> 
> i.e. find surrounding elements of an element.
> 
> With iterators:
> 
> auto iter = container.find(elem);
> auto elembefore = iter - 1;
> auto elemafter = iter + 1;
> 
> Assuming incrementing and decrementing an iterator is checked for 
> out-of-bounds.

The problem is that last statement - "Assuming". If the iterator is the 
first or the last, or if there's only 1 or 2 elements in the container, 
it's crash city. Iterators are *inherently* uncheckable.

For finding the elemafter, it's trivial as find() returns a range from 
the found element to the end (and it's also trivially checkable!).

For elembefore, there's a bit more work involved, probably defining a 
find() that returns a range backed up by one one.



More information about the Digitalmars-d mailing list