Re: need some help with list::end ()



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


.



Relevant Pages

  • Re: need some help with list::end ()
    ... invalidates the iterator to the deleted object. ... I have the following declaration: ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Object creation vs. ThreadLocal retrieval
    ... Object prev = null, current = null; ... Iterator it=list.iterator; ... threadlocal lookup is an orderhashmap lookup. ... public class MyThread extends Thread { ...
    (comp.lang.java.programmer)
  • Re: need some help with list::end ()
    ... Ok I'll try the *remove* method of the list and if I still cant get the ... invalidates the iterator to the deleted object. ... filenames.remove (*prev); ... that's the canonical way to iterate through a list removing some elements ...
    (microsoft.public.dotnet.languages.vc)
  • Re: need some help with list::end ()
    ... invalidates the iterator to the deleted object. ... I have the following declaration: ... filenames.remove (*prev); ... that's the canonical way to iterate through a list removing some elements as ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Puzzle about SGI STLs list iretator !
    ... >PV prev; ... >How does the iterator construct when call beginfunction? ... but it receive only one class type in ...
    (microsoft.public.vc.stl)

Quantcast