Re: iterator invalidation trouble

From: Alexander Stippler (stip_at_mathematik.uni-ulm.de)
Date: 12/29/03


Date: Mon, 29 Dec 2003 19:31:48 +0100

Ron Natalie wrote:

>
> "Alexander Stippler" <stip@mathematik.uni-ulm.de> wrote in message
> news:3ff061a1@news.uni-ulm.de...
>> I think this problem is rather general. Are there any threads online on
>> "robust iterators for c++"? Is there a trick for my situation?
>
> You say the user shouldn't know if the container has changed, however
> since you expose all the ugliness to him, he does have to know.
>
> It's hard to understand what solution to propose without a better
> description of the problem. Why is the user responsible for
> iterating some function f() over a list when f() is not under
> their control. Possibly this is better solved by passing the
> container v (or begin and end iterators) to f() and letting it
> manage searching over the list itself.

As an example of my application, let v be an arithmetic sparse vector, where
I only want to store non zero elements. Now I have e.g.
operator-(SparseVector v, double c), which could internally have a loop
using an iterator _it_ to add c to every non-zero-element. Here is the
client code - I do not want to have to consider the possibility of deleting
elements in every function using an iterator.
If c == *_it_, this would result in *_it_ == 0 and thus the current item is
removed. This happens immediately, when assigning *_it_ its new value
(through a proxy).
That's it. How to handle this? I considered not deleting *_it_ immediately,
but then I would have to guess the right time to do it. Is there any
simpler solution than registering iterators with their container?

regards,
        alex



Relevant Pages

  • Re: iterator invalidation trouble
    ... >> You say the user shouldn't know if the container has changed, ... >client code - I do not want to have to consider the possibility of deleting ... >simpler solution than registering iterators with their container? ... a vector of pointers to heap-allocated objects, ...
    (comp.lang.cpp)
  • Re: Under what circumstances can the STL move a vector?
    ... > vector in the first element on the heap is moved, thus invalidating ... Each container has well defined semantics on how and when iterators ... and references into the container become invalidated. ...
    (comp.lang.cpp)
  • Re: Interface of the set classes
    ... > container usually depend on the data set. ... > templates, iterators, probably tags, and on. ... Python has iterators, and doesn't need templates, since signature based ... The equivalent of "specializing a template" ...
    (comp.lang.python)
  • Re: Reinventing the iterator
    ... this environmental benefit from iterators. ... the container once from beginning to end. ... iterator-based algorithms are good for any ... (defmethod find (item (container array)) ...
    (comp.lang.lisp)
  • Re: Seed7 Release 2007-05-07
    ... You do not need iterators ... I am sure it is possible to declare ... Iterators in Seed7. ... I meant different container types. ...
    (comp.lang.misc)

Loading