STL, list, iterator
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开发者_StackOverflow社区 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!
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...
}
精彩评论