AdamDH
February 13th, 2008, 01:57 PM
std::list<ClassA*> m_List;
std::list<ClassA*>::iterator l_Iter;
m_list.push_back(new ClassA(1));
m_list.push_back(new ClassA(2));
m_list.push_back(new ClassA(3));
m_list.push_back(new ClassA(4));
m_list.push_back(new ClassA(5));
m_list.push_back(new ClassA(6));
for (l_Iter = m_List.begin(); l_Iter != m_List.end(); l_Iter++)
{
if ((*l_Iter)->IsOdd())
{
// Delete classes that have odd numbers
delete (*l_Iter);
m_List.erase(l_Iter);
// QUESTION: How should I adjust the iterator to be valid?
}
}
In the case above I am deleting all ClassA's that have odd numbers assigned to them. When one is found the ClassA object is deleted from memory and the iterm is removed from the list. Once this happens the iterator being used in the for loop is no longer valid and must be adjusted so that the loop can continue. What's the best way to do this?
Instead of a loop, I've thought about using remove_if with a predicate that looks for ClassA's with odd numbers, and using the returned ForwardIterator to end() to delte the ClassA's it found. I have seen list has it's own remove_if function but it does NOT return a ForwardIterator so I cannot rely on this. Is the STL remove_if valid for a std::list?
I'm sure there is a simple solution to this, I am just not seeing it.
I am assuming I could also do something like this as well:
for (l_Iter = m_List.begin(); l_Iter != m_List.end(); l_Iter++)
{
if ((*l_Iter)->IsOdd())
{
// save the item in the list before this one
std::list<ClassA*>::iterator l_TempIter = l_Iter--;
// Delete classes that have odd numbers
delete (*l_Iter);
m_List.erase(l_Iter);
l_Iter = l_TempIter;
}
}
Thanks for your input,
Adam
std::list<ClassA*>::iterator l_Iter;
m_list.push_back(new ClassA(1));
m_list.push_back(new ClassA(2));
m_list.push_back(new ClassA(3));
m_list.push_back(new ClassA(4));
m_list.push_back(new ClassA(5));
m_list.push_back(new ClassA(6));
for (l_Iter = m_List.begin(); l_Iter != m_List.end(); l_Iter++)
{
if ((*l_Iter)->IsOdd())
{
// Delete classes that have odd numbers
delete (*l_Iter);
m_List.erase(l_Iter);
// QUESTION: How should I adjust the iterator to be valid?
}
}
In the case above I am deleting all ClassA's that have odd numbers assigned to them. When one is found the ClassA object is deleted from memory and the iterm is removed from the list. Once this happens the iterator being used in the for loop is no longer valid and must be adjusted so that the loop can continue. What's the best way to do this?
Instead of a loop, I've thought about using remove_if with a predicate that looks for ClassA's with odd numbers, and using the returned ForwardIterator to end() to delte the ClassA's it found. I have seen list has it's own remove_if function but it does NOT return a ForwardIterator so I cannot rely on this. Is the STL remove_if valid for a std::list?
I'm sure there is a simple solution to this, I am just not seeing it.
I am assuming I could also do something like this as well:
for (l_Iter = m_List.begin(); l_Iter != m_List.end(); l_Iter++)
{
if ((*l_Iter)->IsOdd())
{
// save the item in the list before this one
std::list<ClassA*>::iterator l_TempIter = l_Iter--;
// Delete classes that have odd numbers
delete (*l_Iter);
m_List.erase(l_Iter);
l_Iter = l_TempIter;
}
}
Thanks for your input,
Adam