List Iterators, erase() -- help

This is a discussion on List Iterators, erase() -- help within the C++ Programming forums, part of the General Programming Boards category; yes..i understand..basically if I wanted to remove 10, the last element on the list and i incremented it, I am ...

  1. #16
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yes..i understand..basically if I wanted to remove 10, the last element on the list
    and i incremented it, I am going out of bounds..which...
    thanks :P
    You ended that sentence with a preposition...Bastard!

  2. #17
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by anon View Post
    That increment is pointless, since the value assigned to the left-hand side is the result of the function call anyway.

    Perhaps this was meant.

    Code:
    list.erase(iter++);
    This not incrementing an invalid iterator has to do with sequence points: side-effects have to be evaluated before entering the function body.

    BTW, whether this is a right thing to do or not depends on the container. E.g don't try this with a vector.

    If you want to erase elements from the entire list, then remove erases elements with a particular value and remove_if erases elements for which a condition is true. You'll only need a manual loop for a task more complicated, and I don't think there's much new to invent about how that looks.
    Yeah, I get the concept of remove and remove_if, as for the code, I'm not sure why it's pointless. The concept simply was, erase the thing being pointed to (5), take the returned iterator, which is one after the erased one (6), decriment it (4), continue, for loop header goes through, 4 --> 6, else you get the side-effect of erasing 5, then it's 6, then for loop makes i 7, 6 gets skipped.

    I'm not sure what that sequence point stuff is based on the first 2 sentences of that wikpedia, nor how it only applies to C and C++. Pretty much any programming language the order in which you do things matters...if it didn't programming would be much different..that's like the premise of logic, obviously B cannot happen before A if the condition is A. Think I'm missing something, there..

    EDIT:
    Yeah, that does work, but it creates another problem, that's what if the element is first in the list, then decrementing it makes it invalid. So I wouldn't say the code is pointless, but it does create other problems..
    /EDIT

    Quote Originally Posted by Eman View Post
    since this is going on..i don't still understand why I can't just increment the iterator after erasing it..i really get the point of the else

    if erase returns the element after the element to be deleted in question(in this case 5..so it should return the address that points to 6, right)..in the if statement i should be able to do..

    Code:
         while(iter!=list.end())
         {
             if(*iter==5)
            {
                 iter = list.erase(iter) ;
                 cout << *iter ;
            }
    
            iter++ ;
            cout << *iter ;
         }
    If I do that I get a seg fault...I don't get it..that should be perfectly safe..And I've been reading that a list is sequential in memory..i even printed it out myself..and the addresses using the STL list template are freaking sequential..so why do I get a access violation, when I try that?
    If your list is only 5 long, then after you erase, the iter will be pointing to the end() iterator. You increment it, it's invalid, you can't dereference it.

    If your list is more than 5 (or you're not erasing the last element) I see no reason anything is wrong. Should only happen with the max element.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21