Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Questions on safe use of std containers

Reply
Thread Tools

Questions on safe use of std containers

 
 
Jayden Shui
Guest
Posts: n/a
 
      12-20-2011
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
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      12-20-2011
Jayden Shui <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      12-20-2011
On Dec 20, 3:18*am, Jayden Shui <(E-Mail Removed)> wrote:
> 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/.

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.
 
Reply With Quote
 
Fred Zwarts \(KVI\)
Guest
Posts: n/a
 
      12-20-2011
"Juha Nieminen" wrote in message
news:4ef04194$0$4383$(E-Mail Removed)...
>
>Jayden Shui <(E-Mail Removed)> wrote:
>> 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 understanding is that also the use of "reserve" invalidates pointers and
references.

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


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      12-20-2011
"Fred Zwarts \(KVI\)" <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-20-2011
On Tue, 2011-12-20, Goran wrote:
> On Dec 20, 3:18*am, Jayden Shui <(E-Mail Removed)> wrote:
>> 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/.
>
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are sequence containers not a subset of general containers? Sebastian Mach C++ 5 10-06-2012 07:54 PM
Containers of iterators vs. containers of references clark.coleman@att.net C++ 7 01-25-2008 01:37 PM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Questions about destructors on std library containers Ross Boylan C++ 12 02-13-2004 03:03 AM
Is it a right way for using containers std::list< std::string > strList ? Anton C++ 1 08-06-2003 02:10 PM



Advertisments