# Thread: List Iterators, erase() -- help

1. 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

2. Originally Posted by anon
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

Originally Posted by Eman
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.