* Thomas Maier-Komor:
Alf P. Steinbach wrote: * deancoo:
If I have a container, say a vector, with 5 elements, and I initialize
iterator variables to point to the beginning and end of the container, are
those iterators going to always be valid if I replace the elements of the
container? The size stays the same. It seems to be working now, but I
thought I read somewhere something about this not always working out. Any
thoughts?
Replacement is OK, insertion or deletion is not.
yes, but only for a vector. a map or set will resort its content
and therefore invalidate iterators...
Nope.
However, you're right the OP wrote "container", I didn't see that.
In general the question's answer depends on the container, that's why I didn't
see that (like, if I have a hungry animal, say a dog that's got no food for
three days, will it eat a raw steak?).
My imaging system has powerful nonsense-filtering built-in... ;-)
For associative containers, that's maps and sets:
§23.1.2/8: "The insert members shall not affect the validity of
iterators and references to the container, and the erase member shall
invalidate only iterators and references to the erased elements."
There is no resorting requirement, and if resorting takes place §23.1.2/8
restricts its effect on iterators and references.
Replacement for an associative container is to replace the value of a pair,
not the key (which cannot be replaced), and that does not affect iterators or
references. Replacement for a vector or list or queue (not only vector) does
not effect iterators and references. In short, value replacement is OK
(unless one argumentatively regards swap as a replacement), insertion and
deletion depends on the container in question.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?