Re: need some help with list::end ()
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Wed, 16 Aug 2006 07:47:33 -0700
Carl Daniel [VC++ MVP] wrote:
Abubakar wrote:
Sorry for the late reply.
How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.
I use the "remove" method.
I have the following declaration:
list<char * >::const_iterator name, prev;
at some point I do:
prev = name;
name ++;
filenames.remove ( *prev );
and this is how I remove the items.
If you have duplicates in your list, the call to remove will remove
more than one of them at a time, including possibly the one now
referenced by "name". Use the loop that Bo suggested:
if (some_condition)
filename = filenames.erase(filename);
else
++filename;
that's the canonical way to iterate through a list removing some
elements as you go.
Also, how are you adding the items to the list? Since you're storing raw
char*'s, the list isn't managing the memory occupied by the strings - you
are. You should consider changing to a std::list<std::string> instead.
-cd
.
- Follow-Ups:
- Re: need some help with list::end ()
- From: Abubakar
- Re: need some help with list::end ()
- References:
- need some help with list::end ()
- From: Abubakar
- Re: need some help with list::end ()
- From: Bo Persson
- Re: need some help with list::end ()
- From: Abubakar
- Re: need some help with list::end ()
- From: Carl Daniel [VC++ MVP]
- need some help with list::end ()
- Prev by Date: Re: need some help with list::end ()
- Next by Date: Re: Unicode characters in identifiers
- Previous by thread: Re: need some help with list::end ()
- Next by thread: Re: need some help with list::end ()
- Index(es):
Relevant Pages
|