questions on STL containers

Nov 15, 2009 at 11:14am
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 Nov 15, 2009 at 11:20am
Nov 15, 2009 at 1:37pm
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?
Nov 16, 2009 at 1:42am
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?
Nov 16, 2009 at 1:48am
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?
Nov 16, 2009 at 1:51am
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.