Quick question...

D

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?

d
 
A

Alf P. Steinbach

* 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.
 
T

Thomas Maier-Komor

Alf said:
* deancoo:



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...
 
A

Alf P. Steinbach

* Thomas Maier-Komor:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top