Are iterators and ranges going to co-exist?

Steven Schveighoffer schveiguy at yahoo.com
Tue Jul 20 06:49:43 PDT 2010


On Mon, 19 Jul 2010 17:54:59 -0400, Mafi <mafi at example.org> wrote:

> I have to say that I'm not a specialist in STL or C++ in general but as  
> far as I know an iterator is class mainly consisting of a pointer to the  
> container, the current index and the operator-overloadd-function for ++.
>
> So what can you do with a iterator that you can't do with
>    KeyType delegate(KeyType,Container /* which is indexed by KeyType */)

No, an iterator is a pointer to an *element* in the container.  The lookup  
is a simple dereference, not an indexing operation.  They even work on  
containers that don't support indexing.

The benefit of iterators is speed of lookup and reference to a single  
element.  Ranges represent a pair of iterators, with the benefit that they  
are always properly ordered and are related.  Many STL functions accept 2  
iterators but are undefined if the two iterators are not related or  
ordered properly.  Ranges have a huge benefit over iterators for this, and  
most of STL's algorithm is based on pairs of iterators.  However, ranges  
generally refer to *two* elements, a begin and an end element.  This has  
some disadvantages when you want to only refer to one element.

-Steve


More information about the Digitalmars-d mailing list