questions on STL containers

if I declare something like
1
2
3
4
5
6
7
8

set<int> s;
set<int>::iterator i;

s.insert( 4 );
i = s.begin();
s.insert( 1 );


1) when I cout << *i << endl;, I find that it still points to 4 which means that the iterator i still remains pointed at the original begin() not the new begin().
now my question would be is this behaviour consistent(always happens) and also manifest in the other STL containers like map, vector, list, dequeue, etc?
as in would the iterator always point to where it was originally pointed to even when elements are inserted in its position?

2) Another question I have would be do lists of iterators take up alot of memory?
Last edited on
1) It depends on the container, each works in a different way. For example, std::vector may require all the elements to be moved to perform the insertion, so old iterators won't be valid

2) What?
1) so is it advisable to take advantage of this behaviour? as in will it not work or work differently on different compilers with different implementations of the same container?
1.) It depends. If the STL set states that "insert will not invalidate iterators" then you can do it, otherwise, you shouldn't.

2.) Probably the same as a list of pointers...but why would you want to do that?
1) where can I find the info as to whether the STL set or any other container states that "insert will not invalidate iterators" or not?
Topic archived. No new replies allowed.