473,773 Members | 2,398 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
17 7559
This version hangs by the thinnest of threads, but it does still
work.


I do not understand what you mean. :?
Jul 23 '05 #11

"Gernot Frisch" <Me@Privacy.net > schrieb im Newsbeitrag
news:39******** *****@individua l.net...
This version hangs by the thinnest of threads, but it does still
work.


I do not understand what you mean. :?


I _do_ understand now, but I don't understand how it can be working
now ;)

m.erase(it++); is the same as:
m_erase(it); it++;
right?
But after deleting at 'it', I can't '++' it anymore, can I? Does the
std say so?
Jul 23 '05 #12
On Tue, 8 Mar 2005 17:19:42 +0100
"Gernot Frisch" <Me@Privacy.net > wrote:
m.erase(it++); is the same as:
m_erase(it); it++;
right?
But after deleting at 'it', I can't '++' it anymore, can I? Does the
std say so?


No, it is rather like this:

SomeIiterator tmp = it;
++it;
m.erase(tmp);

There is sequence point before erase() call, so it is already
incremented before call starts. Additionally, the erase() call gets old
(i.e. non-incremented) value as an argument.

--
Best regards from
Kamil Burzynski
Jul 23 '05 #13
> 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.


list has a resize?! Oh wait.. resize(), not reserve(). I think the OP was
more concerned as to whether list keeps "dead" nodes around (ie: if you
erase() an element from the list, does the capacity() (assuming list had a
capacity() member) remain constant, or does it go down by 1). No, list
doesn't keep dead nodes around. (Is that mandated by Standard, I'm not
sure).
Jul 23 '05 #14

"Andre Kostur" <nn******@kostu r.net> schrieb im Newsbeitrag
news:Xn******** *************** ********@207.35 .177.135...
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.


list has a resize?! Oh wait.. resize(), not reserve(). I think the
OP was
more concerned as to whether list keeps "dead" nodes around (ie: if
you
erase() an element from the list, does the capacity() (assuming list
had a
capacity() member) remain constant, or does it go down by 1). No,
list
doesn't keep dead nodes around. (Is that mandated by Standard, I'm
not
sure).


Actually my question was: will pointers to other elements stay when I
remove/insert an element. (vector won't, list will keep pointers
valid)
-Gernot
Jul 23 '05 #15
"Gernot Frisch" <Me@Privacy.net > wrote in news:397qcfF5uq 1i4U1
@individual.net :
Actually my question was: will pointers to other elements stay when I
remove/insert an element. (vector won't, list will keep pointers
valid)
-Gernot

Are you sure? In the following code fragment:

list::iterator itr;
list mylist;
for(itr = mylist.begin(); itr != mylist.end(); ++itr) {
// delete the last elemant in the list
}

What happens to the "itr != mylist.end()" test?

BTW, I'm not being smart, I don't know, but suspect
that it is highly compiler dependent.

Jul 23 '05 #16
>> Actually my question was: will pointers to other elements stay when
I
remove/insert an element. (vector won't, list will keep pointers
valid)
-Gernot

Are you sure? In the following code fragment:

list::iterator itr;
list mylist;
for(itr = mylist.begin(); itr != mylist.end(); ++itr) {
// delete the last element in the list
}

What happens to the "itr != mylist.end()" test?

BTW, I'm not being smart, I don't know, but suspect
that it is highly compiler dependent.


What I meant is:

X* pX = &mylist.first() ;
mylist.push_bac k(some_X); // Add some more data
mylist.erase(++ mylist.begin() ); // Remove the 2nd item

pX -> DoSomething(); // valid if 'mylist' is std::list, propably
invalid it it's std::vector

-Gernot
Jul 23 '05 #17
Peter Gordon <petergo@_delet eme_.netspace.n et.au> wrote in
news:Xn******** *************** ***********@203 .10.110.105:
"Gernot Frisch" <Me@Privacy.net > wrote in news:397qcfF5uq 1i4U1
@individual.net :
Actually my question was: will pointers to other elements stay when I
remove/insert an element. (vector won't, list will keep pointers
valid)
-Gernot

Are you sure? In the following code fragment:

list::iterator itr;
list mylist;
for(itr = mylist.begin(); itr != mylist.end(); ++itr) {
// delete the last elemant in the list
}

What happens to the "itr != mylist.end()" test?


mylist.end() points to the one-past-the-end element. In this particular
case, it doesn't really matter since .end() is reevaluated every time you
go through the loop.

Nitpick, if you happen to be on the last element when you delete the last
element in the list, this loop will exhibit undefined behaviour since when
you delete the last element, the itr variable becomes an invalidated
iterator. Then you try to increment it, which is undefined behaviour.

Jul 23 '05 #18

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

Similar topics

25
3639
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
4063
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
5856
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
3886
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
3595
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
9867
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
9621
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
9454
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
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
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...
0
5355
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
4012
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
3
2852
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.