Re: std::map - erase+continue
From: Andre Kostur (nntpspam_at_kostur.net)
Date: 03/09/05
- Next message: Andre Kostur: "Re: Reducing build-times for large projects."
- Previous message: Karl Heinz Buchegger: "Re: pointer arithmetic with inheritance"
- In reply to: Peter Gordon: "Re: std::map - erase+continue"
- Next in thread: Andrew Koenig: "Re: std::map - erase+continue"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 09 Mar 2005 16:29:26 GMT
Peter Gordon <petergo@_deleteme_.netspace.net.au> wrote in
news:Xns9614C28EBB7BEpetergonetspacenetau@203.10.110.105:
> "Gernot Frisch" <Me@Privacy.net> wrote in news:397qcfF5uq1i4U1
> @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.
- Next message: Andre Kostur: "Re: Reducing build-times for large projects."
- Previous message: Karl Heinz Buchegger: "Re: pointer arithmetic with inheritance"
- In reply to: Peter Gordon: "Re: std::map - erase+continue"
- Next in thread: Andrew Koenig: "Re: std::map - erase+continue"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|