std :: list遍历+擦除 [英] std::list traversal+erase
问题描述
嗨
我有一些标准清单,我想要遍历。在遍历期间,我想
有条件地删除一些对象。
我现在的代码就像这样:
for(std :: list< myStruct> :: iterator it = myList.begin(); it!= myList.end();)
{
if(it-> someCondition)
{
std :: list< myStruct> :: iterator it2 = it;
++它;
myList.erase(it2);
继续;
}
// ..对myStructs做一些不应该被删除的工作
还有......
++ it;
}
我的问题还有更优雅的解决方案吗?这是非常难看的代码
现在imho。
只需使用myList.erase(it ++);不起作用,因为在擦除行之后,
迭代器不再有效,因此无法再增加。
非常感谢
Hi
I have some std list, I''d like to traverse. During the traversal, I want to
conditionally delete some objects.
My code for that is like this right now:
for (std::list<myStruct>::iterator it=myList.begin();it!=myList.end();)
{
if (it->someCondition)
{
std::list<myStruct>::iterator it2=it;
++it;
myList.erase(it2);
continue;
}
// .. do some work on the myStructs that arent supposed to be deleted
yet...
++it;
}
Is there any more elegant solution for my problem? It''s quite ugly code
right now imho.
Simply using myList.erase(it++); doesnt work because after the erase line,
the iterator isnt valid anymore and thus cant be increased anymore.
Thanks alot
推荐答案
Frank Neuhaus写道:
Frank Neuhaus wrote:
嗨
我有一些标准清单,我我想穿越。在遍历过程中,我希望
有条件地删除一些对象。
我现在的代码是这样的:
for(std :: list< myStruct> :: iterator it = myList.begin(); it!= myList.end();)
{
if(it-> someCondition)
{
std :: list< myStruct> :: iterator it2 = it;
++它;
myList.erase(it2);
继续;
}
// ..对myStructs做一些不应该被删除的工作
还有......
++ it;
}
我的问题还有更优雅的解决方案吗?这是非常难看的代码
现在imho。
只需使用myList.erase(it ++);不起作用,因为在擦除行之后,
迭代器不再有效,因此无法再增加。
Hi
I have some std list, I''d like to traverse. During the traversal, I want
to conditionally delete some objects.
My code for that is like this right now:
for (std::list<myStruct>::iterator it=myList.begin();it!=myList.end();)
{
if (it->someCondition)
{
std::list<myStruct>::iterator it2=it;
++it;
myList.erase(it2);
continue;
}
// .. do some work on the myStructs that arent supposed to be deleted
yet...
++it;
}
Is there any more elegant solution for my problem? It''s quite ugly code
right now imho.
Simply using myList.erase(it++); doesnt work because after the erase line,
the iterator isnt valid anymore and thus cant be increased anymore.
只需使用返回的迭代器:
if(it-> someCondition)
{
it = myList.erase(it);
}
else
{
//做好工作
++ it;
}
Just use the returned iterator:
if (it->someCondition)
{
it = myList.erase(it);
}
else
{
//do the work
++it;
}
Frank Neuhaus写道:
Frank Neuhaus wrote:
嗨
我有一些标准清单,我想要遍历。在遍历期间,我想
有条件地删除一些对象。
Hi
I have some std list, I''d like to traverse. During the traversal, I want to
conditionally delete some objects.
您确定要使用列表吗? std :: vector(或std :: deque)可能
这里是一个更好的选择。
Are you sure you want to use a list? std::vector (or std::deque) might
well be a better choice here.
>
我现在的代码是这样的:
for( std :: list< myStruct> :: iterator it = myList.begin(); it!= myList.end();)
{
if(it-> ; someCondition)
{
std :: list< myStruct> :: iterator it2 = it;
++ it;
myList.erase(it2);
继续;
}
>
My code for that is like this right now:
for (std::list<myStruct>::iterator it=myList.begin();it!=myList.end();)
{
if (it->someCondition)
{
std::list<myStruct>::iterator it2=it;
++it;
myList.erase(it2);
continue;
}
替换为:
if(it-> someCondition)
{
myList.erase(it ++);
}
else
++ it;
Replace with:
if (it->someCondition)
{
myList.erase(it++);
}
else
++it;
>
// ..对不应删除的myStructs做一些工作
但是...
++它;
}
我的问题是否还有更优雅的解决方案?这是非常难看的代码
现在imho。
只需使用myList.erase(it ++);不起作用,因为在擦除行之后,
迭代器不再有效,因此无法再增加。
>
// .. do some work on the myStructs that arent supposed to be deleted
yet...
++it;
}
Is there any more elegant solution for my problem? It''s quite ugly code
right now imho.
Simply using myList.erase(it++); doesnt work because after the erase line,
the iterator isnt valid anymore and thus cant be increased anymore.
这是不正确的。它类似于(伪代码):
迭代器增量(iterator& i)
{
iterator res = i;
i = i + 1;
返回res;
}
所以myList.erase(它++),迭代器在
myList.erase被调用之前递增。
/ Peter
That is not correct. it++ is similar to (pseudocode):
iterator increment(iterator& i)
{
iterator res = i;
i = i + 1;
return res;
}
so in myList.erase(it++), the iterator is incremented before
myList.erase is called.
/Peter
" peter koch" < pe *************** @ gmail.com写信息
新闻:11 *************** *******@m7g2000cwm.googlegro ups.com ...
"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>
Frank Neuhaus写道:
>
Frank Neuhaus wrote:
>嗨
我有一些标准列表,我想遍历。在遍历期间,我希望
有条件地删除一些对象。
>Hi
I have some std list, I''d like to traverse. During the traversal, I want
to
conditionally delete some objects.
您确定要使用列表吗? std :: vector(或std :: deque)可能
这里是一个更好的选择。
Are you sure you want to use a list? std::vector (or std::deque) might
well be a better choice here.
你是否确定要在他删除这样的元素时推荐一个向量
?似乎
给我一个列表是更好的选择。
擦除后的增量其他预增量
idiom不会在矢量上工作。
Are you sure that you want to recommend a vector
when he''s erasing elements like this? Seems
to me that a list is a better choice given that.
The post increment on erase else pre increment
idiom won''t work on a vector.
这篇关于std :: list遍历+擦除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!