Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[C++] Erasing std::map::iterators while walking the container [solved]

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

Not too surprisingly the following code is crashing the executable:
        std::map<unsigned int, unsigned int>::iterator iterator;
        for (iterator = access.begin(); iterator != access.end(); ++iterator)
        {
            access.erase((*iterator).first); // of course, the actual code is not this simple
        }

I'm afraid I have this multiple times in my code, also for vectors. Is this code just as dangerous for vectors as well? Also, what would be a better way of doing this? Should I remember the last iterator, then go to the next, and then erase the remembered iterator? Thanks! [Edited by - Decrius on May 19, 2009 1:55:31 AM]

Share this post


Link to post
Share on other sites
Advertisement
For your code sample, you'd call std::map<>::clear().

If you want to conditionally erase iterators in an associative container you'd use:

for (iter = map.begin(); iter != map.end();) {
if (condition(iter)) container.erase(iter++);
else ++iter;
}

Share this post


Link to post
Share on other sites
Vectors you would use generally use the erase-remove idiom. If for some reason you had to manually loop you'd use:

for (iter = vec.begin(); iter != vec.end();) {
if (condition(iter)) iter = vec.erase(iter);
else ++iter;
}

Share this post


Link to post
Share on other sites
Okay thanks, I guess you meant:

for (iter = vec.begin(); iter != vec.end();) {
if (condition(iter)) iter = vec.erase(iter++);
else ++iter;
}


But I get the point, same as maps that is. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
No, I didn't. The two loops are different.


Oops, just noticed ^^.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Vectors you would use generally use the erase-remove idiom. If for some reason you had to manually loop you'd use:

for (iter = vec.begin(); iter != vec.end();) {
if (condition(iter)) iter = vec.erase(iter);
else ++iter;
}


Hmmm... Wouldn't that cost O(n^2) time? What I have typically done is
for (read_iter = vec.begin(), write_iter = vec.begin(); read_iter != vec.end(); ++read_iter) {
if (!condition(read_iter)) {
*write_iter = *read_iter;
++write_iter;
}
}
vec.resize(write_iter-vec.begin());

Share this post


Link to post
Share on other sites
Ah I see, is that because erase() makes the std::map reorder it's elements? And, wouldn't swapping elements of the std::map break the algorithm it uses to find elements?

Also, doesn't this remove-erase idiom make me copy the values hold by the std::map a lot of times? Hmm, or does std::swap not actually use the copy constructor / assignment operator?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!