List Iterators question

Oct 2, 2009 at 5:29am
Ok I have a list(let's call it list1) and I would like to mark an element in list1.

What I'm doing right now is getting an iterator that marks a position in list1 and assumes that we are marking that element.

Ie.
list<int> list1;
list<int>::iterator iter;
list1.push_back(2);
iter = list1.end();
iter--; //end is a sentinel.

Now iter is pointing to 2. What I'd like to know is that does STL guarantee that as long as that specific element 2 is not removed, iter will always be pointing to that?

Ie. If i do a bunch of adds/removes to list1 but do not touch the element 2 whatsoever, will iter still be pointing to the location of 2.
Oct 2, 2009 at 8:29am
Hey,

iter will be pointing to the specific element as long as you dont move the iterator or delete the specific element.
Oct 2, 2009 at 12:53pm
Yes, you are guaranteed that by STL for the list<> container. In general, iterator guarantees are container-specific.
Topic archived. No new replies allowed.