std :: map - 擦除+继续 [英] std::map - erase+continue

查看:60
本文介绍了std :: map - 擦除+继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重启:

for(std :: map< x,y> :: iterator it = m.begin(); it!= m.end(); ++ it)

{

if(it-> second.isbad())

{

std :: map< x ,y> :: iterator next = it;

++ next;

m.erase(it);

it = next;

//转到重启;

}

}


这不会删除所有不好的。 ..如果我插入goto restart它

有效...


我做错了什么?

-

- Gernot

int main(int argc,char ** argv){printf

("%silto%c%cf%cgl%ssic%ccom%c"," ; ma",58,''g'',64," ba",46,10);}

restart:
for (std::map<x,y>::iterator it = m.begin(); it!=m.end(); ++it)
{
if( it->second.isbad() )
{
std::map<x,y>::iterator next = it;
++next;
m.erase(it);
it=next;
// goto restart;
}
}

this does not remove all bad ones... If I insert a "goto restart" it
works...

What am I doing wrong?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}

推荐答案



" Gernot Frisch" < Me@Privacy.net> ;, haber iletisinde tunlary

yazdy:39 ************* @ individual.net ...

"Gernot Frisch" <Me@Privacy.net>, haber iletisinde tunlary
yazdy:39*************@individual.net...
restart:
for(std :: map< x,y> :: iterator it = m.begin(); it!= m.end(); ++ it)
{
if(it-> second.isbad())
{
std :: map< x,y> :: iterator next = it;
++ next;
m.erase(it);
it = next;
// goto restart;
}

这不会删除所有不好的。 ..如果我插入goto restart它有效......

我做错了什么?


当你打电话给m.erase(it)时,接下来也是无效的。我猜以下

会修复它。


++ next;

x nextkey = next-> first;

m.erase(it);

it = m.find(nextkey); //而不是它=下一个

-
-Gernot
int main(int argc,char ** argv){printf
("%silto% c%cf%cgl%ssic%ccom%c"," ma",58,''g'',64," ba",46,10);}
restart:
for (std::map<x,y>::iterator it = m.begin(); it!=m.end(); ++it)
{
if( it->second.isbad() )
{
std::map<x,y>::iterator next = it;
++next;
m.erase(it);
it=next;
// goto restart;
}
}

this does not remove all bad ones... If I insert a "goto restart" it
works...

What am I doing wrong?

When you call "m.erase(it)", next is also invalidated. I guess the following
would fix it.

++next;
x nextkey = next->first;
m.erase(it);
it = m.find(nextkey); // instead of it=next

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}




" Aslan Kral" < as ********** @ yahoo.com> ;, haber iletisinde tunlary

yazdy:39 ************* @ individual.net ...

"Aslan Kral" <as**********@yahoo.com>, haber iletisinde tunlary
yazdy:39*************@individual.net...

Gernot Frisch < Me@Privacy.net> ;, haber iletisinde tunlary
yazdy:39 ************* @ individual.net ...

"Gernot Frisch" <Me@Privacy.net>, haber iletisinde tunlary
yazdy:39*************@individual.net...
restart:
for(std :: map< x,y> :: iterator it = m.begin(); it!= m.end(); ++ it)
{
if(it - > second.isbad())
{
std :: map< x,y> :: iterator next = it;
++ next;
m.erase( it);
它=下一个;
// goto restart;
}

这不会删除所有不好的...如果我插入goto restart它有效......

我做错了什么?

当你打电话给m.erase(it)时,接下来也是无效的。我想
restart:
for (std::map<x,y>::iterator it = m.begin(); it!=m.end(); ++it)
{
if( it->second.isbad() )
{
std::map<x,y>::iterator next = it;
++next;
m.erase(it);
it=next;
// goto restart;
}
}

this does not remove all bad ones... If I insert a "goto restart" it
works...

What am I doing wrong?

When you call "m.erase(it)", next is also invalidated. I guess the



以下会修复它。

++ next;
x nextkey = next-> first;
m .erase(it);
它= m.find(nextkey); //而不是它= next
我刚才意识到它可能也是因为你在++ next之后增加了迭代器

。也许你应该在while循环中完成它。你明白我的意思了吗?


while(它!= m.end())

{

if(.. )

{

....

++ next; //你已定位到下一个

}

其他

{

++ it; // next

}

}


following would fix it.

++next;
x nextkey = next->first;
m.erase(it);
it = m.find(nextkey); // instead of it=next I just realized that it might also be because you increment the iterator
after ++next. Maybe you should do it in a while loop. You see what I mean?

while (it != m.end())
{
if (..)
{
....
++next; // you have already positioned to next
}
else
{
++it; //next
}
}


-
-Gernot
int main(int argc,char ** argv){printf
("%silto%c%cf%cgl%ssic%ccom%c"," ma",58,''g'',64, " ba",46,10);}

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}




" Aslan Kral" <关于********** @ yahoo.com>写在

新闻:39 ************* @ individual.net:
"Aslan Kral" <as**********@yahoo.com> wrote in
news:39*************@individual.net:

Gernot弗里希" < Me@Privacy.net> ;, haber iletisinde tunlary
yazdy:39 ************* @ individual.net ...

"Gernot Frisch" <Me@Privacy.net>, haber iletisinde tunlary
yazdy:39*************@individual.net...
restart:
for(std :: map< x,y> :: iterator it = m.begin(); it!= m.end(); ++ it)
{
if(it - > second.isbad())
{
std :: map< x,y> :: iterator next = it;
++ next;
m.erase( it);
它=下一个;
// goto restart;
}

这不会删除所有不好的...如果我插入goto restart它有效......

我做错了什么?

当你打电话给m.erase(it)时,接下来也是无效的。我猜
以下会修复它。
restart:
for (std::map<x,y>::iterator it = m.begin(); it!=m.end(); ++it)
{
if( it->second.isbad() )
{
std::map<x,y>::iterator next = it;
++next;
m.erase(it);
it=next;
// goto restart;
}
}

this does not remove all bad ones... If I insert a "goto restart" it
works...

What am I doing wrong?

When you call "m.erase(it)", next is also invalidated. I guess the
following would fix it.




不在地图中它是不被授予的,它在矢量中无效...... 。当

''''被删除时,只有相同元素的迭代器无效。

''next''已经增加到下一个项目('之前'' '它'已经擦除了'b $ b',所以''next''不会以这种方式失效。

但是,正如你在下一条消息中指出的那样,这个代码在for循环中发生了
,因此有两个可能出现的问题:

1)相邻的坏项目不会被删除,因为第二个将是

跳过。

2)如果地图中的最后一项是坏的,则

调用未定义的行为导致迭代器将在

映射中的end()迭代器之后递增。

++ next;
x nextkey = next-> first;
m.erase(it);
它= m.find(nextkey); //而不是it = next



Not in a map it isn''t (granted, it is invalidated in a vector...). When
''it'' is erased, only iterators to the same element are invalidated.
''next'' has already been incremented to the next item (before ''it'' has
been erased), so ''next'' is safe from being invalidated in this manner.
However, as you point out in your next message, this code is happening
within a for loop, so there are two possible issues that crop up:
1) Adjacent bad items won''t get erased, since the second one will be
skipped over.
2) If the last item in the map is bad, undefined behaviour is invoked by
causing the iterator to be incremented past the end() iterator in the
map.

++next;
x nextkey = next->first;
m.erase(it);
it = m.find(nextkey); // instead of it=next




这可能比

原始代码效率低(在时间方面)(纠正后)对于迭代器的双增量)

,因为原始代码只增加迭代器,这个已经校正。代码

将要求系统再次搜索整个地图。



This would probably be less efficient (in terms of time) than the
original code (after correcting for the double increment of the iterator)
as the original code only increments the iterator, this "corrected" code
would require the system to search the entire map again.


这篇关于std :: map - 擦除+继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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