Questions on safe use of std containers

J

Jayden Shui

Hi All,

As I know, it is not safe to reference the elements in a std::vector
by pointers, since their address may change such as with the expansion
of vector.

My questions is that: how about std::set, map, and list? Are they safe
or not by referencing their elements?

Thanks a lot!

Jayden
 
J

Juha Nieminen

Jayden Shui said:
As I know, it is not safe to reference the elements in a std::vector
by pointers, since their address may change such as with the expansion
of vector.

It's unsafe only if you add new elements to the vector while there are
pointers/references pointing to existing elements. Otherwise it's safe
(well, except for the case where you remove an element to which a
pointer/reference was pointing, but that should be obvious).
My questions is that: how about std::set, map, and list? Are they safe
or not by referencing their elements?

It's safe, except of course if you remove the element being pointed.
 
G

Goran

Hi All,

As I know, it is not safe to reference the elements in a std::vector
by pointers, since their address may change such as with the expansion
of vector.

My questions is that: how about std::set, map, and list? Are they safe
or not by referencing their elements?

Yes.

Standard C++ of 2003 says: "The insert members shall not affect the
validity of iterators and references to the container, and the erase
members shall invalidate only iterators and references to the erased
elements" (that's for associative containers, 23.1.2/8).

For a list, it says: "Does not affect the validity of iterators and
references. If an exception is thrown there are no effects." (23.2.2.3
list modifiers).

Goran.
 
F

Fred Zwarts \(KVI\)

"Juha Nieminen" wrote in message
It's unsafe only if you add new elements to the vector while there are
pointers/references pointing to existing elements. Otherwise it's safe
(well, except for the case where you remove an element to which a
pointer/reference was pointing, but that should be obvious).

My understanding is that also the use of "reserve" invalidates pointers and
references.
 
J

Juha Nieminen

Fred Zwarts \(KVI\) said:
My understanding is that also the use of "reserve" invalidates pointers and
references.

Only if the amount reserved exceeds the current capacity, but in that
case yes, definitely.
 
J

Jorgen Grahn

Yes.

Standard C++ of 2003 says: "The insert members shall not affect the
validity of iterators and references to the container, and the erase
members shall invalidate only iterators and references to the erased
elements" (that's for associative containers, 23.1.2/8).

For a list, it says: "Does not affect the validity of iterators and
references. If an exception is thrown there are no effects." (23.2.2.3
list modifiers).

It is also mentioning noting that these things are often called "the
iterator invalidation rules" of the container, and that you /do/ need
documentation which covers this for the containers you're using.

/Jorgen
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top