Boost logo

Boost Users :

From: Jim Lear (jim.lear_at_[hidden])
Date: 2004-12-13 17:42:19



David Abrahams wrote:
Ben Hutchings wrote:
  
Jim Lear wrote:
    
The map iterators are really what tripped me up as an STL newbie.  One 
must understand the iterators are implemented as pairs for maps, and 
pointers for vectors and other things.  By providing an "T 
operator[](iterator)" function for random access and associative 
containers (and for sequences maybe), one could achieve a consistant 
interface.
      
Container-independent code is an impossible and pointless goal.  maps 
have different semantics from other containers.  You may want to iterate 
over just the mapped values, but other people may want to iterate over 
the keys, or over the pairs (and indeed, so may you, in other part of 
your program).  Who's to say which of these is right?  What you can do 
is to write iterator-independent code, then use an iterator adapter to 
convert the iterators over pairs into iterators over the mapped values:
    

Have you seen http://www.zib.de/weiser/vtl/ and
http://www.boost.org/libs/range/index.html?

  
I'm a little overwhelmed by these discussions, so forgive me for my ignorance or incapacity to communicate.  My intention isn't to create container-independent code.  My intention is to create code for maps (and vectors) that looks like what I think code for maps (and vectors) would look like.  Iterators look to me like pointers to linked lists for vectors, and like pairs of pointers for maps.  There is nothing wrong with these semantics, except they seem odd compared to some other languages semantics for associative arrays (e.g. awk).  However, overloading the operator[] in the map class to take iterators as a parameter would allow iterators to be treated like keys, if one desires, which to me seems more natural.  This would still allow one to iterate over keys, mapped values, or pairs.

So in the absolute most abhorently poor code example (let's just call it "meta-code"), the map operator would behave like:

    data_type &operator[](const iterator &i) { return i->second; }

Am I just too ignorant to make any sense? :-)  Maybe I'll crawl back into my hole. :-)
-- 
Jim Lear

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net