Re: Containers of iterators vs. containers of references
On Jan 21, 8:43 pm, clark.cole...@att.net 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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34