STL、列表、迭代器

发布于 2024-09-24 02:33:08 字数 873 浏览 0 评论 0原文

请购买我的英语)))

我有这个问题: 我必须使用可能的编辑项目迭代 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;
    }

有人可以告诉我如何以其他方式做到这一点,而无需明确使用 _Myhead、_Myhead 等。 谢谢!

Exscusme buy my English )))

I have this problem:
I must iterate items in STL list with posible editing items: remove, add, edit.
This is my approach:

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;
    }

Could someone tell me how I can do it in other way without explicity using _Myhead, _Myhead and so on.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-10-01 02:33:08

您是否考虑过使用 list::iteratorlist::const_iterator

for (list<Tree>::iterator i = list.begin(); i != list.end(); ++i) {
    ...do stuff with *i...
}

Have you considered using list<Tree>::iterator and list<Tree>::const_iterator?

for (list<Tree>::iterator i = list.begin(); i != list.end(); ++i) {
    ...do stuff with *i...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文