Containers of iterators vs. containers of references

Discussion in 'C++' started by clark.coleman@att.net, Jan 21, 2008.

  1. Guest

    Relatively new to STL use. I coded up a bunch of container code with
    lots of iterators. Now I question whether I want to have so much
    nested iterator syntax.

    For example, if you are coding a compiler and you have classes such as
    BasicBlock. Each basic block has predecessors and successors in the
    control flow graph. Right now, I am storing these as iterators:

    list<list<BasicBlock>::iterator> Predecessors;
    list<list<BasicBlock>::iterator> Successors;

    This leads to messy looking code. I have a GetFirstPred() method that
    now returns something of type:

    list<list<BasicBlock>::iterator>::iterator

    and likewise a GetLastPred() method that does the same. Now, when I
    want to iterate through all predecessors of a block, I have a loop
    with all kinds of dereferencing of iterators:

    list<list<BasicBlock>::iterator>::iterator CurrPred;
    for (CurrPred = this->GetFirstPred(); CurrPred != this->GetLastPred();
    ++CurrPred) {

    if ((*CurrPred)->IsJoinPoint()) .... etc.
    }

    Should this all be list of references instead of list of iterators for
    simplicity?

    list<BasicBlock &> Predecessors;

    Thanks for any recommendations.
    , Jan 21, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > Relatively new to STL use. I coded up a bunch of container code with
    > lots of iterators. Now I question whether I want to have so much
    > nested iterator syntax.
    >
    > For example, if you are coding a compiler and you have classes such as
    > BasicBlock. Each basic block has predecessors and successors in the
    > control flow graph. Right now, I am storing these as iterators:
    >
    > list<list<BasicBlock>::iterator> Predecessors;
    > list<list<BasicBlock>::iterator> Successors;
    >
    > This leads to messy looking code. I have a GetFirstPred() method that
    > now returns something of type:
    >
    > list<list<BasicBlock>::iterator>::iterator
    >

    Hide the complexity behind typedefs.

    >
    > Should this all be list of references instead of list of iterators for
    > simplicity?
    >
    > list<BasicBlock &> Predecessors;
    >

    You can't have containers of references.

    --
    Ian Collins.
    Ian Collins, Jan 21, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Jan 21, 8:43 pm, wrote:
    > Relatively new to STL use. I coded up a bunch of container code with
    > lots of iterators. Now I question whether I want to have so much
    > nested iterator syntax.


    > For example, if you are coding a compiler and you have classes such as
    > BasicBlock. Each basic block has predecessors and successors in the
    > control flow graph. Right now, I am storing these as iterators:


    > list<list<BasicBlock>::iterator> Predecessors;
    > list<list<BasicBlock>::iterator> Successors;


    Why? I'd just use pointers. (Although list is somewhat of an
    exception, in general, iterators are just too fragile to be of
    much use except for immediately looping.)

    > This leads to messy looking code. I have a GetFirstPred()
    > method that now returns something of type:


    > list<list<BasicBlock>::iterator>::iterator


    Typedef's would help, don't you think? Something like:

    typedef std::list< BasicBlock* >::iterator
    PredecessorIter ;

    (FWIW: I'd probably use an std::vector here, rather than
    std::list.)

    [...]
    > Should this all be list of references instead of list of iterators for
    > simplicity?


    > list<BasicBlock &> Predecessors;


    You can't have a container of references; references aren't
    objects. I would use a container of pointers, however.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jan 22, 2008
    #3
  4. Guest


    > You can't have a container of references; references aren't
    > objects. I would use a container of pointers, however.
    >



    Any recommendations for type of pointer? I started playing with
    shared_ptr but then I discovered that Visual Studio does not yet
    support C++ TR1. I need the code to run on both x86/Linux under g++
    and Visual Studio.
    , Jan 24, 2008
    #4
  5. Jeff F Guest

    <> wrote in message
    news:...
    >
    >> You can't have a container of references; references aren't
    >> objects. I would use a container of pointers, however.

    >
    > Any recommendations for type of pointer? I started playing with
    > shared_ptr but then I discovered that Visual Studio does not yet
    > support C++ TR1. I need the code to run on both x86/Linux under g++
    > and Visual Studio.


    Use the shared_ptr from www.boost.org, which is the basis for TR1, and
    supports both of those compilers along with many others.

    Better yet see the boost graph library which more directly supports your
    application.

    Jeff Flinn
    Jeff F, Jan 25, 2008
    #5
  6. James Kanze Guest

    On Jan 25, 12:44 am, wrote:
    > > You can't have a container of references; references aren't
    > > objects. I would use a container of pointers, however.


    > Any recommendations for type of pointer?


    A pointer to whatever type you wanted the reference to.

    > I started playing with shared_ptr but then I discovered that
    > Visual Studio does not yet support C++ TR1. I need the code to
    > run on both x86/Linux under g++ and Visual Studio.


    I wouldn't bother with shared_ptr except in some special cases.
    I find it fairly rare that a container should control the
    lifetime of an object that isn't copiable (and if the object is
    copiable, of course, the vector will contain objects, and not
    pointers or references)---most of the time, objects which aren't
    copiable are entity objects which control their own lifetime.
    (One exception might be a container of polymorphic agents. In
    such cases, I'll usually use an invasive reference counter in
    the base class.)

    If you do want shared_ptr, of course, you can get it from boost.
    I'm pretty sure that recent versions of both g++ and VC++ can
    handle Boost.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jan 25, 2008
    #6
  7. James Kanze Guest

    On Jan 25, 2:41 am, "Jeff F" <> wrote:
    > <> wrote in message


    > news:...


    > >> You can't have a container of references; references aren't
    > >> objects. I would use a container of pointers, however.


    > > Any recommendations for type of pointer? I started playing with
    > > shared_ptr but then I discovered that Visual Studio does not yet
    > > support C++ TR1. I need the code to run on both x86/Linux under g++
    > > and Visual Studio.


    > Use the shared_ptr fromwww.boost.org, which is the basis for
    > TR1, and supports both of those compilers along with many
    > others.


    > Better yet see the boost graph library which more directly
    > supports your application.


    Looking at his application: he certainly doesn't want
    shared_ptr, since there will be cycles---and in this case, using
    weak_ptr to break them will be fairly complicated. I'm not
    familiar with the Boost graph library, to know whether it's
    appropriate, but if not, raw pointers would seem to be the way
    to go.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jan 25, 2008
    #7
  8. Pete Becker Guest

    On 2008-01-24 18:44:58 -0500, said:

    >
    >> You can't have a container of references; references aren't
    >> objects. I would use a container of pointers, however.
    >>

    >
    >
    > Any recommendations for type of pointer? I started playing with
    > shared_ptr but then I discovered that Visual Studio does not yet
    > support C++ TR1. I need the code to run on both x86/Linux under g++
    > and Visual Studio.


    VC++ 2008 has support for TR1 (excluding C99 and special math
    functions) in their "Feature Pack".

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
    Pete Becker, Jan 25, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

    Iterators as associative containers' key

    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    765
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Jun 26, 2003
  2. Merlin
    Replies:
    4
    Views:
    589
    Merlin
    Apr 30, 2004
  3. progII
    Replies:
    1
    Views:
    327
    progII
    Apr 2, 2005
  4. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    478
    Kai-Uwe Bux
    May 8, 2005
  5. , India
    Replies:
    10
    Views:
    1,062
    James Kanze
    Aug 8, 2009
Loading...

Share This Page