On Tue, 2002-02-12 at 06:21, M. Mueller (bhu5nji) wrote: > What I learned is that a container size must remain constant when an iterator > is being used in a loop guard condition. If this rule cannot be followed, > then the loop must be forcefully aborted. This is a practical rule only.
Hmm... the Josuttis book says this about erase: "For vectors and deques, this operation might invalidate iterators and reference to other elements." Which seems to suggest that iterators of other containers shouldn't be invalidated. I wonder if the spec changed this at the last minute? Hmm... actually looking at the implementation gives some insight, though. Apparently, associative containers are implemented using red-black trees. RB trees, being balanced, will tend to shift quite a bit whenever something is inserted or deleted. That's the most likely reason why erase invalidates the iterators. > The Musser 2nd Ed. book has no example AFAIK showing the potential flaw and a > work around. The only hint is in Section 21.1.5 Sorted Associative Container > Requirements on page 326 where it states, > > "Finally, an *iterator* of a sorted associative container is bidirectional. > The *insert* operations do not affect the validity of the iterators and > references to the container, and *erase* operations invalidate only iterators > and references to the erased elements." > > It appears that the reason that *erase* invalidates iterators is left as an > exercise for the reader. This reader could use a little help, however. > See above. For a non-associative container, however, it's a little bit different. Lists should have no problem. iterators should still work (try it out, if you're curious). For vectors, however, because the memory allocation is dynamic, if an insert or erase triggers an allocation/deallocation in memory, the container could be moved to a different location, and therefore have all it's iterators invalidated. Tanner -- Tanner Lovelace | [EMAIL PROTECTED] | http://wtl.wayfarer.org/ --*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-- GPG Fingerprint = A66C 8660 924F 5F8C 71DA BDD0 CE09 4F8E DE76 39D4 GPG Key can be found at http://wtl.wayfarer.org/lovelace.gpg.asc --*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-- Those who are willing to sacrifice essential liberties for a little order, will lose both and deserve neither. -- Benjamin Franklin History teaches that grave threats to liberty often come in times of urgency, when constitutional rights seem too extravagant to endure. -- Justice Thurgood Marshall, 1989
signature.asc
Description: This is a digitally signed message part
