473,767 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::map - erase+continue

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%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}

Jul 23 '05 #1
17 7555

"Gernot Frisch" <Me@Privacy.net >, haber iletisinde şunları
yazdı:39******* ******@individu al.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;
}
}

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%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}

Jul 23 '05 #2

"Aslan Kral" <as**********@y ahoo.com>, haber iletisinde şunları
yazdı:39******* ******@individu al.net...

"Gernot Frisch" <Me@Privacy.net >, haber iletisinde şunları
yazdı:39******* ******@individu al.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;
}
}

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 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%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}


Jul 23 '05 #3
"Aslan Kral" <as**********@y ahoo.com> wrote in
news:39******** *****@individua l.net:

"Gernot Frisch" <Me@Privacy.net >, haber iletisinde şunları
yazdı:39******* ******@individu al.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;
}
}

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.


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.
Jul 23 '05 #4

"Andre Kostur" <nn******@kostu r.net>, haber iletisinde şunları
yazdı:Xn******* *************** *********@207.3 5.177.134...
"Aslan Kral" <as**********@y ahoo.com> wrote in
news:39******** *****@individua l.net:

"Gernot Frisch" <Me@Privacy.net >, haber iletisinde şunları
yazdı:39******* ******@individu al.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;
}
}

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.


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.


OK.
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.


Right. That was because I thought there might a problem there but later I
realized that was not the case.
Jul 23 '05 #5
so... this ins the best: ?

for(std::map<x, y>::iterator it = m.begin(); it!=m.end(); )
{
if(...)
{
std::map<x,y>:: iterator next = it;
++next;
m.erase(it);
it=next;
}
else
++it;
}
Jul 23 '05 #6

"Gernot Frisch" <Me@Privacy.net >, haber iletisinde şunları
yazdı:39******* ******@individu al.net...
so... this ins the best: ?

for(std::map<x, y>::iterator it = m.begin(); it!=m.end(); )
{
if(...)
{
std::map<x,y>:: iterator next = it;
++next;
m.erase(it);
it=next;
}
else
++it;
}

Maybe. Does it now work the way you want it to? Personally I like "while"
and you seem to prefer "for". No problem.
Jul 23 '05 #7

"Aslan Kral" <as**********@y ahoo.com> schrieb im Newsbeitrag
news:39******** *****@individua l.net...

"Gernot Frisch" <Me@Privacy.net >, haber iletisinde şunları
yazdı:39******* ******@individu al.net...
so... this ins the best: ?

for(std::map<x, y>::iterator it = m.begin(); it!=m.end(); )
{
if(...)
{
std::map<x,y>:: iterator next = it;
++next;
m.erase(it);
it=next;
}
else
++it;
}

Maybe. Does it now work the way you want it to? Personally I like
"while"
and you seem to prefer "for". No problem.


Yes, it works now. Thank you.
BTW: Does a std::map re-allocate memory for items when
inserting/removing, or does it behave like a std::list?
-Gernot
Jul 23 '05 #8

"Gernot Frisch" <Me@Privacy.net >, haber iletisinde şunları
yazdı:39******* ******@individu al.net...

"Aslan Kral" <as**********@y ahoo.com> schrieb im Newsbeitrag
news:39******** *****@individua l.net...

"Gernot Frisch" <Me@Privacy.net >, haber iletisinde şunları
yazdı:39******* ******@individu al.net...
so... this ins the best: ?

for(std::map<x, y>::iterator it = m.begin(); it!=m.end(); )
{
if(...)
{
std::map<x,y>:: iterator next = it;
++next;
m.erase(it);
it=next;
}
else
++it;
}

Maybe. Does it now work the way you want it to? Personally I like
"while"
and you seem to prefer "for". No problem.


Yes, it works now. Thank you.
BTW: Does a std::map re-allocate memory for items when
inserting/removing, or does it behave like a std::list?
-Gernot


map/multimap/set/multiset are based on tree structures and they allocate
when you call insert and free when you call erase/clear. So they don't have
a member function like resize() as in list/vector.

Jul 23 '05 #9
"Gernot Frisch" <Me@Privacy.net > wrote in message
news:39******** *****@individua l.net...
for(std::map<x, y>::iterator it = m.begin(); it!=m.end(); )
{
if(...)
{
std::map<x,y>:: iterator next = it;
++next;
m.erase(it);
it=next;
}
else
++it;
}


You can simplify it:

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

This version hangs by the thinnest of threads, but it does still work.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
3637
by: Christopher Benson-Manica | last post by:
If you liked the functionality of std::map, but found that you couldn't trust your implementation to handle nonstandard data structures, how would you code a wrapper? I've produced the following: #include <map> template < class keytype, class recordtype > class pseudomap {
14
4062
by: Flzw | last post by:
Well I have a map like this : std::map <string, CObject> ObjectList; I have a function like this : CObject* NewObject( char* Name, CArg* Arg) { std::string key = Name; ObjectList = CObject( Name, Arg);
13
1447
by: Peteroid | last post by:
Why does reading a member of a std::map not considered const? For example: class My_Class { int Get_Map_Value( int index ) const // ** error ** not considered const!!! { return m_Map ; // note that m_Map is not changed, only read from }
20
5854
by: Dilip | last post by:
I understand the C++ standard does not talk about threading. My question here is directed more towards what happens when a STL container is used in a certain way. I'd appreciate any thoughts. I re-iterate I don't want to probe into what C++ standard says when I trample some data from multiple threads, I simply want to know if I have understood this right. I have a "std::map<somedatatype, someotherdatatype> myMap", where stuff gets...
25
3885
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It is important that I iterate through the list backwards, because the items in it have to be processed in reverse order before erasing. However, there does not appear to be an std::list::erase() method defined for reverse iterators.
16
3591
by: Frank Neuhaus | last post by:
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) {
6
9866
by: Evyn | last post by:
Hi, How do I compare 2 maps for identical keys, and if they have identical keys, check if they have identical values? In either case I want to copy ONLY that key value pair to one of two different maps. I know how to copy the entire map to another: // Copy fset1 to fset3 std::copy(fset1.begin(),fset1.end(),std::inserter(fset3, fset3.begin()));
6
1395
by: Tobe | last post by:
Hi, Here's an example of something that feels like it should be OK but does in fact produce a segfault on every compiler I've tried (VC2005, g ++ 4.1.2/Linux, g++ 3.4.4/Cygwin). The line marked // KABOOM is the one that segfaults. If I change the references to pointers (and make the appropriate dereferences) everything works just fine so is this some finer point of references I'm not grasping or an issue with the STL ?
3
1168
by: kernus | last post by:
i am looking for a more elegant/standard way to do this simple operation, however, for now, i just coded this beast. any comments will be grateful, ObserverMap::iterator itr = observers_.begin(); while(itr != observers_.end()){ if(itr->second == observer){ ObserverMap::iterator tmpItr = ++itr;
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.