Walking thru a STL list and remove element at the same time?

Discussion in 'C++' started by Allerdyce.John@gmail.com, Feb 1, 2006.

  1. Guest

    In STL list, is it safe to do this:

    list<A> aList; //private attribute of MyClass


    void MyClass:: aMethod() {
    list<A>::Iterator iter;

    for ( iter = aList.begin() ; iter != aList.end() ; iter++) {
    A a = (*iter);

    if ( check(a) ) {

    aList.erase (iter); // is that okay?

    }
    }

    }

    Thank you.
     
    , Feb 1, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > In STL list, is it safe to do this:
    >
    > list<A> aList; //private attribute of MyClass
    >
    >
    > void MyClass:: aMethod() {
    > list<A>::Iterator iter;
    >
    > for ( iter = aList.begin() ; iter != aList.end() ; iter++) {
    > A a = (*iter);
    >
    > if ( check(a) ) {
    >
    > aList.erase (iter); // is that okay?


    Change to iter = aList.erase(iter);

    >
    > }
    > }
    >
    > }
    >
    > Thank you.
     
    , Feb 1, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > wrote:
    > > In STL list, is it safe to do this:
    > >
    > > list<A> aList; //private attribute of MyClass
    > >
    > >
    > > void MyClass:: aMethod() {
    > > list<A>::Iterator iter;
    > >
    > > for ( iter = aList.begin() ; iter != aList.end() ; iter++) {
    > > A a = (*iter);
    > >
    > > if ( check(a) ) {
    > >
    > > aList.erase (iter); // is that okay?

    >
    > Change to iter = aList.erase(iter);


    Oh, and of course you will only want to increment iter if you don't
    delete.
     
    , Feb 1, 2006
    #3
  4. E. Mark Ping Guest

    In article <>,
    <> wrote:
    >Change to iter = aList.erase(iter);


    Which will get you into trouble when you get to iter++ at the for
    loop.

    The full approach is:

    list<A> aList; //private attribute of MyClass

    void MyClass:: aMethod() {
    list<A>::Iterator iter;

    //note no increment in the third clause of the for loop
    for ( iter = aList.begin() ; iter != aList.end() ; ) {
    A a = (*iter);

    if ( check(a) ) {
    iter = aList.erase (iter); // is that okay?
    } else {
    ++iter;
    }
    }

    }


    Or you can simply use remove_if.
    --
    Mark Ping
     
    E. Mark Ping, Feb 1, 2006
    #4
  5. Daniel T. Guest

    In article <>,
    wrote:

    > In STL list, is it safe to do this:
    >
    > list<A> aList; //private attribute of MyClass
    >
    >
    > void MyClass:: aMethod() {
    > list<A>::Iterator iter;
    >
    > for ( iter = aList.begin() ; iter != aList.end() ;
    > iter++) {
    > A a = (*iter);
    >
    > if ( check(a) ) {
    >
    > aList.erase (iter); // is that okay?
    >
    > }
    > }
    >
    > }
    >
    > Thank you.


    aList.erase(aList.begin(),
    remove_if(aList.begin(), aList.end(), &check ));

    But don't do the above if aList contains pointers and you need to delete
    the objects before removal. Of course, if that's the case then you
    should probably wrap them in a smart pointer.

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Feb 2, 2006
    #5
  6. In message <>,
    Daniel T. <> writes
    >In article <>,
    > wrote:
    >
    >> In STL list, is it safe to do this:
    >>
    >> list<A> aList; //private attribute of MyClass
    >>
    >>
    >> void MyClass:: aMethod() {
    >> list<A>::Iterator iter;
    >>
    >> for ( iter = aList.begin() ; iter != aList.end() ;
    >> iter++) {
    >> A a = (*iter);
    >>
    >> if ( check(a) ) {
    >>
    >> aList.erase (iter); // is that okay?
    >>
    >> }
    >> }
    >>
    >> }
    >>
    >> Thank you.

    >
    >aList.erase(aList.begin(),
    > remove_if(aList.begin(), aList.end(), &check ));


    The remove_if algorithm may have to copy many instances of A, which
    could potentially be quite expensive. std::list provides a more
    efficient alternative:

    aList.remove_if(check);


    --
    Richard Herring
     
    Richard Herring, Feb 8, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    614
    benben
    Jan 31, 2006
  2. THTB
    Replies:
    0
    Views:
    206
  3. Max Williams
    Replies:
    3
    Views:
    180
    Robert Klemme
    Jan 6, 2009
  4. Joseph
    Replies:
    8
    Views:
    107
    Joseph
    Mar 22, 2005
  5. Joseph
    Replies:
    0
    Views:
    78
    Joseph
    Mar 19, 2005
Loading...

Share This Page