Deleting in STL List

Started by
9 comments, last by AdmiralBinary 22 years, 2 months ago
remove doesn''t actually delete anything. It returns an iterator to the end of the new valid sequence...here''s an example.

Let''s say we have a vector vec:
1,2,3,3,2,3,4,5,x
(x is the end of the container,undefined)

and we call remove(vec.begin(),vec.end(),3)

we get:
1,2,2,4,5,(q),q,q,x
where the parenthesized q is the value returned by remove.
Calling vec.end() still returns the last value. The value of q is undefined, but in this case, it is probably 5(usually the value that gets copied over).
Typically you want to erase the elements you remove, so the complete call looks like this:
vec.erase(remove(vec.begin(),vec.end(),3),vec.end());

This topic is closed to new replies.

Advertisement