Sign in to follow this  

Another STL list question

This topic is 3857 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

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

This topic is 3857 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this