列表中的条件擦除元素 - 在循环中 [英] Conditional erasing elements from list - in a loop

查看:76
本文介绍了列表中的条件擦除元素 - 在循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一年前有人在这里问过:如何在一个循环中从列表中删除所选元素

?答案如下:

for(it = l.begin(); it!= l.end;){

if(...)

it = l.erase(it);

else

++ it;

}

我使用STL列表,我也这样做:

for(it = l.begin(); it!= l.end; ++ it){

if(...)l.erase(it--);

}

这样可以吗?或者它是否依赖于详细信息

列表实现?

是否保证运营商 -

在失效前被调用the itarator?


问候 -

OC

Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?

regards -
O.C.

推荐答案

" Tescobar" <醇************** @ gmail.com>在消息中写道

news:7a ****************************** @ localhost.ta lkaboutprogramming。 com ...
"Tescobar" <ol**************@gmail.com> wrote in message
news:7a******************************@localhost.ta lkaboutprogramming.com...
一年多以前有人问过:如何在循环中从列表中删除选定的元素?答案如下:

for(it = l.begin(); it!= l.end;){
if(...)
it = l .erase(it);

++ it;
}

我使用STL列表,我这样做:

for(it = l.begin(); it!= l.end; ++ it){
if(...)l.erase(it--);
}

这样可以吗?或者它是否依赖于列表实现的细节?
是否可以保证在失效的itarator之前调用运算符?
Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?



''运算符 - ''必须在调用''erase''之前调用,因为''运算符 - ''的结果会被传递给''erase'' 。所以我不明白为什么这个

不行。虽然第一个版本更清晰,至少和第二个版本一样快。


问候

-

jb


(rot13中的回复地址,先解读)


''operator --'' must be called before ''erase'' is called, because the
result of ''operator --'' is passed to ''erase''. So I do not see why this
would not be ok. Though the first version is clearer and at least as
fast as the second version.

regards
--
jb

(reply address in rot13, unscramble first)


Tescobar写道:
Tescobar wrote:
[..]
for(it = l.begin(); it!= l.end; ++ it){


我希望你的意思


for(it = l.begin(); it!= l.end(); ++ it){

if(...)l .erase(it--);
}

这样可以吗? [...]
[..]
for( it = l.begin(); it != l.end; ++it ) {
I hope you meant

for( it = l.begin(); it != l.end(); ++it ) {
if(...) l.erase(it--);
}
Is that OK? [...]




编号如果第一个元素要删除,你会使''它''无效

by递减它超过'l.begin()''。再增加它确实

不一定让它恢复有效 - 未定义的行为。


V



No. If the first element is to be erased, you will make ''it'' invalid
by decrementing it past the ''l.begin()''. Incrementing it again does
not necessarily bring it back to be valid -- undefined behaviour.

V


在2005-07-21 13:01:04 -0400,Tescobar <醇************** @ gmail.com>说:
On 2005-07-21 13:01:04 -0400, "Tescobar" <ol**************@gmail.com> said:
一年前有人在这里问:如何在循环中从列表中删除选定的元素?答案如下:

for(it = l.begin(); it!= l.end;){
if(...)
it = l .erase(it);

++ it;
}

我使用STL列表,我这样做:

for(it = l.begin(); it!= l.end; ++ it){
if(...)l.erase(it--);
}

这样可以吗?或者它是否依赖于列表实现的细节?
是否可以保证在失效的itarator之前调用运算符?
Over one year ago somebody asked here: how to
remove selected elements from list
in a loop?. The answer was as follows:
for( it = l.begin(); it != l.end; ) {
if(...)
it = l.erase(it);
else
++it;
}
I use STL list, and I do the same like this:
for( it = l.begin(); it != l.end; ++it ) {
if(...) l.erase(it--);
}
Is that OK? Or does it depend on details
of the list implementation ??
Has it to be guaranteed that the operator --
is called before invalidation of the itarator?




想想当列表中的第一个项目被擦除时会发生什么(你将从列表的开头运行)。另外,如果你
可以将你的if()的条件变为谓词,那么你可以将
减少到整个东西:

l.remove_if(谓语);


-

Clark S. Cox,III
cl ******* @ gmail.com



Think about what happens when the very first item in the list is being
erased (you''ll run off the beginning of the list). Additionally, if you
can make the condition of your if() into a predicate, then you could
reduce the whole thing down to:

l.remove_if(predicate);

--
Clark S. Cox, III
cl*******@gmail.com


这篇关于列表中的条件擦除元素 - 在循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆