std::list remove element mid iteration

C

Christopher

The situation is that a std::list<std::set<std::string> > is being
iterated through. Upon certain criteria some sets become empty. I need
to remove the empty sets from the list.

Is it safe to iterate through a list and call list::erase( iterator )
in mid iteration?
 
R

red floyd

Christopher said:
The situation is that a std::list<std::set<std::string> > is being
iterated through. Upon certain criteria some sets become empty. I need
to remove the empty sets from the list.

Is it safe to iterate through a list and call list::erase( iterator )
in mid iteration?

Well, you can use

struct set_is_empty
{
bool operator()(const std::set& s) const { return s.empty(); }
};

std::erase(std::remove_if(l.begin(), l.end(), set_is_empty());

or else, the canonical iteration for this list is:

for (it = l.begin(); it != l.end(); )
if (it->empty())
it = l.erase(it);
else
++it;
 
R

red floyd

red said:
Well, you can use

struct set_is_empty
{
bool operator()(const std::set& s) const { return s.empty(); }
};

std::erase(std::remove_if(l.begin(), l.end(), set_is_empty());
crap. That should be:

l.erase(std::remove_if(l.begin(), l.end(), set_is_empty()), l.end());
 
J

James Kanze

Well, you can use
struct set_is_empty
{
bool operator()(const std::set& s) const { return s.empty(); }
};
std::erase(std::remove_if(l.begin(), l.end(), set_is_empty());

Which could be unnecessarily expensive. In the case of
std::list, the canonical form is:

l.remove_if( set_is_empty() ) ;

However, the original poster said that sets "become" empty
during his iteration, so this can't be used.
or else, the canonical iteration for this list is:
for (it = l.begin(); it != l.end(); )
if (it->empty())
it = l.erase(it);
else
++it;

Adopted to his case, you'd add braces and put the if at the end
of the loop. (Also, I'd write this with a while, rather than a
for. Something like:

std::list<...>::iterator iter = l.begin() ;
while ( iter != l.end() ) {
// processing...
if ( iter->empty() ) {
iter = l.erase( iter ) ;
} else {
++ iter ;
}
}

I'd prefer even more if that if could be replaced with a ?: on
the right side of an assignment, since the most important aspect
here is the update of the iterator, and not how it's being
updated, but I can't think of a nice way of doing this off hand.
 
C

cgspwei

Which could be unnecessarily expensive. In the case of
std::list, the canonical form is:

l.remove_if( set_is_empty() ) ;
That is what I found in MSDN,
remove_if is a STL algorithm which removes all elements from the range
(First,Last) that cause the predicate to return true. It returns an
iterator equal to Last - n, where n = number of elements removed. The
last n elements of the range have undefined values. The size of the
container remains the same.
But there is a method named remove_if in std::list.
template<class Predicate>
void remove_if(
Predicate _Pred
)
Erases elements from a list for which a specified predicate is
satisfied.
I didn't know there is a method named remove_if in the std::list. Can
any one told me why there is no similar method in vector ?

Thanks in advance,:)
 
J

James Kanze

That is what I found in MSDN,
remove_if is a STL algorithm which removes all elements from the range
(First,Last) that cause the predicate to return true. It returns an
iterator equal to Last - n, where n = number of elements removed. The
last n elements of the range have undefined values. The size of the
container remains the same.
But there is a method named remove_if in std::list.
template<class Predicate>
void remove_if(
Predicate _Pred
)
Erases elements from a list for which a specified predicate is
satisfied.
I didn't know there is a method named remove_if in the std::list. Can
any one told me why there is no similar method in vector ?

Because you don't need it, and it's not directly supported by
the underlying data structure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top