这样为什么会报错误
CStringList var(15);
POSITION position;
position=var.InsertAfter(NULL,"item1");
position=var.InsertAfter(position,"item3");
position=var.InsertBefore(position,"item2");
for(position=var.GetHeadPosition();NULL!=position;var.GetNext(position))
{
CString str;
str=var.GetAt(position);
if("item3"==str)
{
var.RemoveAt(position);
}

}而这么写却不会有问题呢?CStringList var(15);
POSITION position;
position=var.InsertAfter(NULL,"item1");
position=var.InsertAfter(position,"item3");
position=var.InsertBefore(position,"item2");
for(position=var.GetTailPosition ();NULL!=position;var.GetPrev(position))
{
CString str;
str=var.GetAt(position);
if("item3"==str)
{
var.RemoveAt(position);
}

}

解决方案 »

  1.   

    错误出现在 var.RemoveAt(position);
      

  2.   

    改为
    if("item3"==str)
    {
    var.RemoveAt(position);
    break;
    }break为跳出循环
      

  3.   

    CStringList var(15);
    POSITION position;
    position=var.InsertAfter(NULL,"item1");
    position=var.InsertAfter(position,"item3");
    position=var.InsertBefore(position,"item2");
    for(position=var.GetTailPosition ();NULL!=position;var.GetPrev(position))
    {
    CString str;
    str=var.GetAt(position);
    if("item3"==str)
    {
    var.RemoveAt(position);
    }

    }
    此时执行第3此后position=var.GetTailPosition ();==》position=NULL
    不满住NULL!=position条件,所以跳出for循环
      

  4.   

    错误不是因为 var.RemoveAt(position);
    而是在for(position=var.GetHeadPosition();NULL!=position;var.GetNext(position))
    造成指针越界