List 列表,迭代器

List 列表,迭代器,list,stl,iterator,List,Stl,Iterator,Exscusme购买我的英语) 我有一个问题: 我必须迭代STL列表中的项目,其中包含可编辑的项目:删除、添加、编辑。 这是我的方法: void MoveOnNewPlace(Tree& parent, Tree& child) { Tree *temp=&parent; while(temp->GetParent()!=temp && temp->GetItem().GetChar()=='(') temp=

Exscusme购买我的英语)

我有一个问题: 我必须迭代STL列表中的项目,其中包含可编辑的项目:删除、添加、编辑。 这是我的方法:

void MoveOnNewPlace(Tree& parent, Tree& child)
{

    Tree *temp=&parent;
    while(temp->GetParent()!=temp && temp->GetItem().GetChar()=='(')
        temp=temp->GetParent();
    if(*temp==parent) return;
    Tree newTr(child);
    newTr.SetParent(*temp);
    temp->GetSubNodes()->push_back(newTr); //(Tree(child,temp));
    parent.GetSubNodes()->remove(child);

}

list<Tree>::_Nodeptr ptr=nodes->_Myhead->_Next;
    unsigned int i=0;
    while(i++<nodes->size() && ptr!=0)
    {
        Prepair(ptr->_Myval);
        if(ptr->_Myval.GetItem().GetChar()=='[')
            MoveOnNewPlace(t,ptr->_Myval);
        ptr=ptr->_Next;
    }
void MoveOnNewPlace(树和父树、树和子树)
{
树*temp=&parent;
而(temp->GetParent()!=temp&&temp->GetItem().GetChar()=='(')
temp=temp->GetParent();
如果(*temp==父项)返回;
树蝾螈(儿童);
新设置父项(*温度);
temp->GetSubNodes()->推回(newTr);/(树(子节点,temp));
parent.GetSubNodes()->删除(子节点);
}
列表::_nodeptrptr=nodes->_Myhead->_Next;
无符号整数i=0;
而(i++size()&&ptr!=0)
{
Prepair(ptr->u Myval);
如果(ptr->_Myval.GetItem().GetChar()=='[')
移动位置(t,ptr->u Myval);
ptr=ptr->\u下一步;
}
有没有人能告诉我,我怎么能不使用_Myhead、_Myhead等等明确地用其他方式来做呢。
谢谢!

您是否考虑过使用
list::iterator
list::const\u iterator

for (list<Tree>::iterator i = list.begin(); i != list.end(); ++i) {
    ...do stuff with *i...
}
for(list::iterator i=list.begin();i!=list.end();++i){
…用…我。。。
}
如果使用list::const_迭代器,则会出现编译错误;如果使用list::迭代器,则会出现运行时错误,因为列表中的项可能会被删除,从而导致内存访问冲突。