Boost logo

Boost Users :

From: Jim Lear (jim.lear_at_[hidden])
Date: 2004-12-10 11:07:55


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.  So, a better example of what tripped me up is multidimensional associative arrays:

    #include <map>
    #include <string>
    #include <iostream>
   
    void main() {
      typedef map<string, int> si_map_t;
      typedef map<string, si_map_t> ssi_map_t;
      ssi_map_t table;
      table["0"]["0"]=20;
      table["0"]["1"]=21;
      table["1"]["0"]=10;
      table["1"]["1"]=11;
   
      for (ssi_map_t::iterator i=table.begin(); i!=table.end(); i++)
        for (si_map_t::iterator j=i->second.begin(); j!=i->second.end(); j++)
          cout << j->second << endl;
      // Blech! :-)  This is quite obscure for newbies.

    }

Instead, the following is how I felt the inner loop should be implemented (as it is in other languages with associative arrays):

    for (si_map_t::iterator j=table[i].begin(); j!=table[i].end(); j++)
       cout << table[i][j] << endl;

Granted, it's more inneficient with the two operators, but the code is easy to read and write and consistant with the map's common semantics.


David Abrahams wrote:
goochrules! wrote:
  
I know this is not the correct place to suggest new features to the STL,
but I can't find a similar group for STL.  Any suggestions?

Sorry for the interruption.

P.S. In case you're wondering what the suggestion is: Add ability to use
random access operators with iterators.  E.g.

vector<int> intv;
...
vector<int>::iterator i=intv.begin();
intv[i]++;
      
What do you want that last line to do?  To me it looks like you could
be using "(*i)++;" instead to achieve the same result.

    
It seems odd and confusing that iterators have pointer semantics but not
random access semantics.
      
they do already, well, some of them at least do (of which, vector is
one): http://www.sgi.com/tech/stl/RandomAccessIterator.html.  
    

Random access iterators do have random access semantics, but that
particular expression won't compile with some (mutable) random access
iterators.  The problem is that the result of p[i] is only required to
be convertible to the iterator's value_type.

Cheers,

  

-- 
Jim Lear

This document/email may contain confidential, proprietary or privileged
information and is intended only for use of the addressee/recipient named
above. No confidentiality or privilege is waived or lost by any error in
transmission of this document/email.  If you are not the intended recipient
of this document/email of Legerity, Inc., you are hereby notified that you
must not use, disseminate, or copy it in any form or take any action in
reliance on it.

If you have received this document/email in error, please immediately
notify me at (512) 228-5760 and permanently destroy/delete the original
document/email and any copy of the document/email and printout thereof.


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