Jump to content
  • Advertisement
Sign in to follow this  
etsuja

Another STL list question

This topic is 4072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

will an iterator be assigned as list.begin() before anything has been added to the list generate an error in program execution?

Share this post


Link to post
Share on other sites
Advertisement
No, it will not generate an error unless you try and dereference it. When the container is empty, list.begin() == list.end().

Note, however, that the iterator will probably be invalidated when you modify the list, though. (edit: apparently the iterator is not invalidated, according to the c.2007 standard draft)


jfl.

Share this post


Link to post
Share on other sites
When you ask for the begin() iterator of an empty container, the iterator returned is the same as the end() iterator.
You cannot dereference the end() iterator or your program will misbehave or crash.
Therefore, you cannot dereference the iterator from begin() of an empty list.

Share this post


Link to post
Share on other sites
Quote:
Original post by jflanglois
(edit: apparently the iterator is not invalidated, according to the c.2007 standard draft)


As you say, it won't be invalidated because the iterator points to a node in the list, and unlike vectors, memory is not shuffled when data is added or removed (unless of course you remove the item the iterator is pointing too!).

That imo is the only benifit of using an STL list over a vector or deque (due to the insert/remove speed of elements in the middle of the list).

Spree

[Edited by - SpreeTree on May 26, 2007 6:11:45 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by SpreeTree
As you say, it won't be invalidated because the iterator points to a node in the list, and unlike vectors, memory is not shuffled when data is added or removed (unless of course you remove the item the iterator is pointing too!).

Spree

Actually, I made a mistake. The iterator he wants to use is invalid anyway.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!