Sign in to follow this  
DaTroof

C++: Erasing Elements in a Set

Recommended Posts

DaTroof    162
I have a routine that iterates through a set of object pointers, and performs a function that determines whether the element should be removed. It looks something like this:
set<foo*>::iterator i = container.begin();
foo* bar;
while (i != container.end()) {
        bar = *i;
	if (bar->execute() == 0) {
		i++;
	} else {
		container.erase(i);
		i = container.upper_bound(bar);
	}
}

This is the best way I could conceive to remove elements from the set while iterating through it, the main problem being that I don't see another way to keep the iterator valid after the erase(). Is there a better method?

Share this post


Link to post
Share on other sites
SiCrane    11839
Not in for std::set in standard C++, though some vendors have std::set::erase() return an iterator anyways. Anyways one way to handle this is to make a copy of the iterator, increment that and then erase the old iterator.

Share this post


Link to post
Share on other sites
DaTroof    162
Quote:
Original post by SiCrane
Not in for std::set in standard C++, though some vendors have std::set::erase() return an iterator anyways. Anyways one way to handle this is to make a copy of the iterator, increment that and then erase the old iterator.


Yes, the std::set::erase() I'm using doesn't return an iterator. Copying the iterator is probably more efficient than an upper_bound check, so I'll try that instead. Thanks.

Share this post


Link to post
Share on other sites
Talria    102
You could maintain iterator to previous element (prev_i, initially container.end()) and do:

if(prev_i==container.end())
i=container.begin();
else
{
i=prev_i;
++i;
}




once it's being removed from the set.

Edit: Or what SiCrane suggested. It's actually better option.

Share this post


Link to post
Share on other sites

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