[gameprogrammer] Re: Iterator question/organize a vector

Alan Wolfe wrote:
I know some people think STL rules, but of the game programmers ive
talked to about it, nobody seems to really use it.

Works great here :-) Most of the other game programmers I've spoken with in Austin seem to be happy with it, too. I know a fair number of people still working in C, but... well, they're just crazy.

IMO this is a case in point where STL isn't so hot; It's unclear the
right way to do a simple operation - something you could have coded
yourself in just a few minutes (:

First of all, http://www.sgi.com/tech/stl/Vector.html is a great resource.

Second of all... STL or no, that seems like a very complicated and inefficient way to swap two elements. Vectors provide *linear time insertion and removal* of elements in the middle of the array. Both the insert() and erase() calls are walking forward from where iter1 is, moving each element up and then down one element.

Here's how I would do it, and I think even Alan will agree that it's a fairly clear and intuitive way of doing it:

        // using indices, which are neither faster nor slower than iterators,
        // but more clear in this case
        std::string temp = word[idx1];
        word[idx1] = word[idx2];
        word[idx2] = temp;

And finally, to answer the question directly (using the SGI STL reference):

        A vector's iterators are invalidated when its memory is reallocated.
        Additionally, inserting or deleting an element in the middle of a
        vector invalidates all iterators that point to elements following
        the insertion or deletion point.
--
 Matthew Weigel
 hacker
 unique & idempot.ent

---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: