利用CPtrList管理一堆对象,现在需要遍历链表,删除其中符合要求的结点,代码如下:POSITION pos = m_accountlist.GetHeadPosition();
for(int i = 0; i<m_accountlist.GetCount();i++)
{
CAccount* pAccount = (CAccount*)m_accountlist.GetNext(pos);
if(pAccount->m_name == "abc")
{
m_accountlist.GetPrev(pos);
m_accountlist.RemoveAt(pos);
return;
}
}说明一下:m_accountlist是CPtrList类型的链表,里面都是CAccount类型的指针。现在遍历链表,查找m_name 为 "abc"的元素并删除。由于GetNext(pos)函数会使pos移到下一个元素的位置,所以我先调用GetPrev(pos)使pos回到刚才找到的正确位置上,然后在调用RemoveAt进行删除。对于多个元素时开始是正确的,但当链表只有一个元素时,GetPrev(pos)就会报错,因为此时的pos是一个无效的位置。如果不加m_accountlist.GetPrev(pos);则删除的元素是要删除元素的下一个元素,就不对了。请问应该如何处理呢?
for(int i = 0; i<m_accountlist.GetCount();i++)
{
CAccount* pAccount = (CAccount*)m_accountlist.GetNext(pos);
if(pAccount->m_name == "abc")
{
m_accountlist.GetPrev(pos);
m_accountlist.RemoveAt(pos);
return;
}
}说明一下:m_accountlist是CPtrList类型的链表,里面都是CAccount类型的指针。现在遍历链表,查找m_name 为 "abc"的元素并删除。由于GetNext(pos)函数会使pos移到下一个元素的位置,所以我先调用GetPrev(pos)使pos回到刚才找到的正确位置上,然后在调用RemoveAt进行删除。对于多个元素时开始是正确的,但当链表只有一个元素时,GetPrev(pos)就会报错,因为此时的pos是一个无效的位置。如果不加m_accountlist.GetPrev(pos);则删除的元素是要删除元素的下一个元素,就不对了。请问应该如何处理呢?
{
CAccount* pAccount = (CAccount*)m_accountlist.GetAt(i);
if(pAccount->m_name == "abc")
{
m_accountlist.RemoveAt(i);
delete pAccount;
return;
}
}
判断一下元素是否为空
// 这样试一下,随手写的,没有测试
POSITION pos = m_accountlist.GetTailPosition();
while(pos)
{
CAccount* pAccount = (CAccount*)m_accountlist.GetAt(pos);
if(pAccount->n_name == "abc")
{
m_accountlist.RemoveAt(pos);
}
m_accountlist.GetPrev(pos);
}
while(pos)
{
CAccount* pAccount = (CAccount*)m_accountlist.GetAt(pos);
if(pAccount->n_name == "abc")
{
m_accountlist.RemoveAt(pos);
}
if(m_accountlist.GetCount() == 0)
{
break;
} m_accountlist.GetPrev(pos);
}好像基本就没问题了,不知是否可取?