Re: iterator invalidation trouble
From: Dan W. (danw_at_raytron-controls.com)
Date: 12/29/03
- Next message: Jeff Schwab: "Re: What kind of problems is c++ best at solving?"
- Previous message: Rolf Magnus: "Re: What kind of problems is c++ best at solving?"
- In reply to: Alexander Stippler: "Re: iterator invalidation trouble"
- Next in thread: Howard Hinnant: "Re: iterator invalidation trouble"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 29 Dec 2003 13:52:26 -0500
On Mon, 29 Dec 2003 19:31:48 +0100, Alexander Stippler
<stip@mathematik.uni-ulm.de> wrote:
>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
You mean you are subtracting a number from all these numbers, and
removing them once they become zero? What about if they become
negative? You also say "if( c == *it )" but you cannot compare floats
or doubles for equality like that. And which iterator(s) become(s)
non-valid? You mean that you have multiple iterators in other parts
of the code pointing at elements in this container as you're deleting
elements.
You haven't explained basic things, like, do you need the elements to
be sorted? Random accessible? Find-able in constant time?
There are containers, for instance hash_set and hash_map, which have
the advantage that iterators are not invalidated by insertion or
deletion of elements. Or you could achieve something similar by using
a vector of pointers to heap-allocated objects, then you keep pointers
to particular objects, rather than iterators.
HTH
- Next message: Jeff Schwab: "Re: What kind of problems is c++ best at solving?"
- Previous message: Rolf Magnus: "Re: What kind of problems is c++ best at solving?"
- In reply to: Alexander Stippler: "Re: iterator invalidation trouble"
- Next in thread: Howard Hinnant: "Re: iterator invalidation trouble"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|