Whenever a caller or client modifies a library collection class, all iterators are made invalid (with the exception of the iterator through which the change was made, in the case of remove). Give a reason for this rule in the case of an array list (using a dynamically sized array, in which an iterator has a pointer to the array) and in the case of list (using linked list nodes, in which case the iterator points to one or more list nodes).
closed as off-topic by xskxzr, Juho, Evil, David Richerby, hengxin Dec 22 '18 at 4:01
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – xskxzr, Evil, David Richerby, hengxin
The question is "why".
There are two problems: First, you would have to define how an iterator that doesn't become invalid would change if the underlying container changes. That will be an interesting problem. Some languages have iterators that can iterate through hash tables, enumerating all (key, value) pairs in some order. Now imagine you insert an item into the hash table, the number of items becomes too large, and the whole hash table is re-hashed, and you need to define how the iterator is modified in a meaningful way.
Second, if the iterators need to stay valid, you need to keep track somehow which iterators are involved with which underlying container. That's not too difficult, but costs time and space. Together with the first problem, it's much easier to just declare any iterator as undefined after a change.
PS. If you copy / paste this as an answer to a homework question, your teacher will most likely trace it back to this post.