STL std :: map erase [英] STL std::map erase

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

问题描述




您好,


我有一个问题是从std :: map中删除键值对,而<迭代它。
迭代它。


根据STL文档,擦除地图中的元素会使指向该元素的所有

迭代器无效


所以


for(map< ...> :: iterator i = mymap.begin(); i!= mymap。 end(); i ++){

if(test(i)){

mymap.erase(i);

mymap.insert( newelement);

}

}


看起来像是不好的做法,它会在执行时崩溃,至少<在一台机器上我已经运行了这样的代码。


所以我的问题是,如何方便地删除当前代码?当我在迭代它的时候,在一张

地图中的元素? (我承认这听起来好像用C ++听起来像英文一样奇怪)?


Thanx,


Pieter



Hello,

I''ve got a question conerning erasing key-value pairs from a std::map while
iterating over it.

According to the STL docs, erasing an element in a map invalidates all
iterators pointing to that element

so

for (map<...>::iterator i = mymap.begin(); i != mymap.end(); i++) {
if (test(i)) {
mymap.erase(i);
mymap.insert(newelement);
}
}

looks like bad practice, and it does crash when it gets executed, at least
on one machine i''ve run such code on.

So my question is, how do I conveniently erase the "current" element in a
map on the fly while I''m iterating over it? (I admit that this sounds as
weird in English as it sounds in C++)?

Thanx,

Pieter

推荐答案

Pieter Thysebaert写道:
Pieter Thysebaert wrote:

你好,
<我有一个问题是从std :: map中删除键值对,同时重复它。

根据STL文档,删除一个元素map无效指向该元素的所有
迭代器



for(map< ...> :: iterator i = mymap.begin(); i != mymap.end(); i ++){
if(test(i)){
mymap.erase(i);
mymap.insert(newelement);
}
}

看起来像是不好的做法,它会在执行时崩溃,至少在一台机器上运行这样的代码。

所以我的问题是,如何方便地删除当前消息。我正在迭代它的地图中的元素? (我承认在英语中听起来像C ++听起来一样奇怪)?

Hello,

I''ve got a question conerning erasing key-value pairs from a std::map while
iterating over it.

According to the STL docs, erasing an element in a map invalidates all
iterators pointing to that element

so

for (map<...>::iterator i = mymap.begin(); i != mymap.end(); i++) {
if (test(i)) {
mymap.erase(i);
mymap.insert(newelement);
}
}

looks like bad practice, and it does crash when it gets executed, at least
on one machine i''ve run such code on.

So my question is, how do I conveniently erase the "current" element in a
map on the fly while I''m iterating over it? (I admit that this sounds as
weird in English as it sounds in C++)?




那么真正的问题是什么?

问题是,在使用iterator i擦除元素之后,你需要再次使用
迭代器,增量来获取下一个map元素。

As你知道这是无效的。所以一个可能的解决方案是:在下一个元素的迭代器中获取你的手

,*在你擦除之前。


-

Karl Heinz Buchegger
kb******@gascad.at




" Pieter Thysebaert" < PI *************** @ toorug.muchac.spambe>写在

消息新闻:c8 ********** @ gaudi2.UGent.be ...

"Pieter Thysebaert" <pi***************@toorug.muchac.spambe> wrote in
message news:c8**********@gaudi2.UGent.be...


你好,

我有一个问题,在迭代它时,从std :: map
中删除键值对。

根据STL文档,擦除地图中的元素会使指向该元素的所有
迭代器无效



for(map< ...> :: iterator i = mymap .begin(); i!= mymap.end(); i ++){
if(test(i)){
mymap.erase(i);
mymap.insert(newelement) ;
}

看起来像是不好的做法,它会在执行时崩溃,至少在一台机器上运行我已运行此类代码。

所以我的问题是,如何方便地删除当前消息。我正在迭代它的地图中的元素? (我承认在英语中听起来像C ++听起来一样奇怪)?


Hello,

I''ve got a question conerning erasing key-value pairs from a std::map while iterating over it.

According to the STL docs, erasing an element in a map invalidates all
iterators pointing to that element

so

for (map<...>::iterator i = mymap.begin(); i != mymap.end(); i++) {
if (test(i)) {
mymap.erase(i);
mymap.insert(newelement);
}
}

looks like bad practice, and it does crash when it gets executed, at least
on one machine i''ve run such code on.

So my question is, how do I conveniently erase the "current" element in a
map on the fly while I''m iterating over it? (I admit that this sounds as
weird in English as it sounds in C++)?




for(map< ...> :: iterator i = mymap.begin(); i!= mymap.end();){

if(test(i)){

mymap.erase(i ++);

mymap.insert(newelement);

} else {

++ i;

}

}


理解这段代码需要正确理解''i ++'的运作方式。


john



for (map<...>::iterator i = mymap.begin(); i != mymap.end(); ) {
if (test(i)) {
mymap.erase(i++);
mymap.insert(newelement);
} else {
++i;
}
}

Understanding this code requires a proper understanding of how ''i++'' works.

john




" John Harrison" <乔************* @ hotmail.com>在消息中写道

news:2ghjh6F2s3b7U1 @ uni-> >

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2ghjh6F2s3b7U1@uni-> >
所以我的问题是,如何方便地删除当前消息。
中的元素在飞行时我正在迭代它? (我承认在英语中听起来像C ++听起来一样奇怪)?
So my question is, how do I conveniently erase the "current" element in a map on the fly while I''m iterating over it? (I admit that this sounds as
weird in English as it sounds in C++)?



for(map< ...> :: iterator i = mymap.begin) (); i!= mymap.end();){
if(test(i)){
mymap.erase(i ++);
mymap.insert(newelement);
} else {
++ i;
}


理解这段代码需要正确理解''i ++''



for (map<...>::iterator i = mymap.begin(); i != mymap.end(); ) {
if (test(i)) {
mymap.erase(i++);
mymap.insert(newelement);
} else {
++i;
}
}

Understanding this code requires a proper understanding of how ''i++''



有效。


好​​的......那么解释怎么样?


我怎么看不到这有什么不同。当擦除调用结束时,指向该元素的所有

迭代器都不再有效。并且不允许等待擦除调用返回,然后

执行该增量?在那种情况下,我不能递增,因为它已经无效了,对吧?对于这样的实现,在

函数参数中递增i应该与在

语句中递增它相同,据我所知。


这两种情况都不能为实现创建相同的代码吗?...


foo(i ++);

vs.


f(i);

i ++;


我可以看到实施可能不是*要求*使那些

相同,但是不允许*使它们相同吗?


我是什么?在这里不见了?


-Howard


works.

Ok...so how about an explanation?

I don''t see how this is any different. When the call to erase finishes, all
iterators pointing to that element are no longer valid. And isn''t an
implementation allowed to wait until the erase call returns, and then
perform that increment? In that case, i cannot be incremented because it is
invalid already, right? For such an implementation, incrementing i in the
function parameter ought to be the same as incrementing it in the for
statement, as far as I can see.

Can''t an implementation create identical code for both these cases?...

foo(i++);

vs.

f(i);
i++;

I could see that the implementation might not be *required* to make those
the same, but would it not be *allowed* to make them the same?

What am I missing here?

-Howard


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

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