how to erase a list member with just an iterator (without the list itself)?

Discussion in 'C++' started by alon, Jan 14, 2007.

  1. alon

    alon Guest

    I got the following situation: I have a few lists of integers, and I
    have an iterator pointing to one of the elements. I don't have a
    pointer to the list itself ! All I want is to remove the element the
    iterator points to...

    Basically, a list element has pointers back and forth, so I shouldn't
    need the list itself, but I don't see any function that does the work.
    Is there a real problem to do this? Is it just a coincidence that no
    body implemented such a function?
     
    alon, Jan 14, 2007
    #1
    1. Advertising

  2. alon

    alon Guest

    Should make it clear - talking about STL list..

    alon wrote:
    > I got the following situation: I have a few lists of integers, and I
    > have an iterator pointing to one of the elements. I don't have a
    > pointer to the list itself ! All I want is to remove the element the
    > iterator points to...
    >
    > Basically, a list element has pointers back and forth, so I shouldn't
    > need the list itself, but I don't see any function that does the work.
    > Is there a real problem to do this? Is it just a coincidence that no
    > body implemented such a function?
     
    alon, Jan 14, 2007
    #2
    1. Advertising

  3. alon

    Milind Guest

    Hi,

    http://www.sgi.com/tech/stl/List.html
    This says, there is a erase function. and it says it does exactly what
    you are expecting out of it..

    Couldn't get your question really.

    ~M
    alon wrote:
    > I got the following situation: I have a few lists of integers, and I
    > have an iterator pointing to one of the elements. I don't have a
    > pointer to the list itself ! All I want is to remove the element the
    > iterator points to...
    >
    > Basically, a list element has pointers back and forth, so I shouldn't
    > need the list itself, but I don't see any function that does the work.
    > Is there a real problem to do this? Is it just a coincidence that no
    > body implemented such a function?
     
    Milind, Jan 14, 2007
    #3
  4. alon

    alon Guest

    I'll try and clarify my question:

    The erase function requires a pointer to the list.
    list<int> L;
    list<int>::iterator iter;
    L.erase(iter);

    In my case there is no pointer to the list, so what I need is something
    like this:
    list<int>::erase(iter);

    Does this clarify my question?


    Milind wrote:
    > Hi,
    >
    > http://www.sgi.com/tech/stl/List.html
    > This says, there is a erase function. and it says it does exactly what
    > you are expecting out of it..
    >
    > Couldn't get your question really.
    >
    > ~M
    > alon wrote:
    > > I got the following situation: I have a few lists of integers, and I
    > > have an iterator pointing to one of the elements. I don't have a
    > > pointer to the list itself ! All I want is to remove the element the
    > > iterator points to...
    > >
    > > Basically, a list element has pointers back and forth, so I shouldn't
    > > need the list itself, but I don't see any function that does the work.
    > > Is there a real problem to do this? Is it just a coincidence that no
    > > body implemented such a function?
     
    alon, Jan 14, 2007
    #4
  5. alon

    red floyd Guest

    Re: how to erase a list member with just an iterator (without thelist itself)?

    alon wrote:
    > I'll try and clarify my question:
    >
    > The erase function requires a pointer to the list.


    No, erase is a member function of the list, and therefore requires a
    list object.

    > list<int> L;
    > list<int>::iterator iter;
    > L.erase(iter);
    >
    > In my case there is no pointer to the list, so what I need is something
    > like this:
    > list<int>::erase(iter);



    Nope. How does erase know what list to erase from? Again, erase is a
    *MEMBER FUNCTION* and therefore requires the object.
     
    red floyd, Jan 14, 2007
    #5
  6. alon

    Daniel T. Guest

    "alon" <> wrote:

    > I got the following situation: I have a few lists of integers, and I
    > have an iterator pointing to one of the elements. I don't have a
    > pointer to the list itself ! All I want is to remove the element the
    > iterator points to...
    >
    > Basically, a list element has pointers back and forth, so I shouldn't
    > need the list itself, but I don't see any function that does the work.
    > Is there a real problem to do this? Is it just a coincidence that no
    > body implemented such a function?


    It cannot be done, and that was by design. Removing an element from a
    list without notifying the list is poor practice and makes some list
    optimizations difficult to implement. For example, a list may contain a
    "size_" member variable in order to speed up the call to myList.size().
    Removing a element without telling the list would corrupt that member.

    Of course, if each list iterator had a pointer to the list it belonged
    too, then it could be done, but the C++ committee apparently decided not
    to do that.
     
    Daniel T., Jan 14, 2007
    #6
  7. alon

    alon Guest

    "if each list iterator had a pointer to the list " - in practice, this
    is what I ended up doing :)

    Thx,
    Alon


    Daniel T. wrote:
    > "alon" <> wrote:
    >
    > > I got the following situation: I have a few lists of integers, and I
    > > have an iterator pointing to one of the elements. I don't have a
    > > pointer to the list itself ! All I want is to remove the element the
    > > iterator points to...
    > >
    > > Basically, a list element has pointers back and forth, so I shouldn't
    > > need the list itself, but I don't see any function that does the work.
    > > Is there a real problem to do this? Is it just a coincidence that no
    > > body implemented such a function?

    >
    > It cannot be done, and that was by design. Removing an element from a
    > list without notifying the list is poor practice and makes some list
    > optimizations difficult to implement. For example, a list may contain a
    > "size_" member variable in order to speed up the call to myList.size().
    > Removing a element without telling the list would corrupt that member.
    >
    > Of course, if each list iterator had a pointer to the list it belonged
    > too, then it could be done, but the C++ committee apparently decided not
    > to do that.
     
    alon, Jan 15, 2007
    #7
    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:
    6
    Views:
    652
    Jim Langston
    Oct 30, 2005
  2. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    369
    Pete Becker
    Mar 30, 2006
  3. Dalbosco J-F
    Replies:
    3
    Views:
    885
    Marcus Kwok
    Aug 3, 2006
  4. Replies:
    6
    Views:
    465
    Howard
    Dec 4, 2006
  5. Krice
    Replies:
    10
    Views:
    855
    James Kanze
    Oct 30, 2008
Loading...

Share This Page