jacob said:
Ian Collins a écrit :
Of course! I am ALWAYS wrong
But maybe you care to explain why?
I can.
It is one reason why I said it would be a
It is very simple. ALL operations that modify the container other
than through the iterator invalidate all iterators to that container.
When you use an iterator over a container, you are allowed to
o Read anything even from different iterators.
o Write through a single iterator. THAT iterator (the one you use
to modify the container) is NOT invalidated. ALL others are.
This is different to C++ where the rules go for pages and pages.
I don't know C++ rules for this kind of thing, but I'll describe a real
situation I've got. In my case it is implemented using a database, but
it could equally well be implemented with a list...
I have one job which is repeatedly starts at the beginning of the list,
goes through it sometimes changing state fields on some of the records,
sometimes deleting them, sometimes adding new items to the *end* of the
list (i.e. not the current node the iterator is on.
There is a logically separate task which could be interspersed with the
first task either by being a separate thread, or being a function which
is called between steps through the list described above. This iterates
through a second list, and under some conditions added entries to the
end of the first list.
For this particular program, restarting the iterator on the first list
when items are added to the end of the list would be completely the
*wrong* thing to do (but it's what would happen with your described
implementation), and in some situations could actually cause an
effective lockup preventing things from being done where my customer
actually gets fined if the job isn't done.
The first list I talked about was a list of jobs to be run. Sometimes
the jobs won't complete (and might continue to not complete for a long
time). Sometimes the jobs decide to submit more jobs to be run later.
The second list is a list of jobs to be scheduled at defined time
intervals (anything from once a minute to once a day).