S
s
Here is a snippet of my code:
<code>
list< MyClass * >outgoing_pool;
MyClass* GetWaitingObject(string freq)
{
MyClass *temp_ptr = NULL;
list<MyClass*>::iterator i;
for(i=outgoing_pool.begin(); i!=outgoing_pool.end(); i++)
{
if( (*i)->GetFrequency() == freq ) //match
{
temp_ptr = *i;
//remove item from list
outgoing_pool.erase(i);
}
}
return temp_ptr;
}
</code
This function is supposed to iterate through the list and remove a match
(if found). However, I'm getting a segmentation fault on the
(*i)->GetFrequency() call. This only happens after an element has been
erase()'ed out of "outgoing_pool". Does the iterator i need to be
"updated" or some such after a call to erase? I was thinking that it
was smart enough to handle this but now I'm not so sure...
I guess my question is (not totally related to the above code, but...)
what is a good way to iterate through a std::list and remove selected
elements?
Thanks,
S
<code>
list< MyClass * >outgoing_pool;
MyClass* GetWaitingObject(string freq)
{
MyClass *temp_ptr = NULL;
list<MyClass*>::iterator i;
for(i=outgoing_pool.begin(); i!=outgoing_pool.end(); i++)
{
if( (*i)->GetFrequency() == freq ) //match
{
temp_ptr = *i;
//remove item from list
outgoing_pool.erase(i);
}
}
return temp_ptr;
}
</code
This function is supposed to iterate through the list and remove a match
(if found). However, I'm getting a segmentation fault on the
(*i)->GetFrequency() call. This only happens after an element has been
erase()'ed out of "outgoing_pool". Does the iterator i need to be
"updated" or some such after a call to erase? I was thinking that it
was smart enough to handle this but now I'm not so sure...
I guess my question is (not totally related to the above code, but...)
what is a good way to iterate through a std::list and remove selected
elements?
Thanks,
S