[gameprogrammer] Re: Iterator question/organize a vector

  • From: "David Olsen" <jolynsbass@xxxxxxxxx>
  • To: <gameprogrammer@xxxxxxxxxxxxx>
  • Date: Mon, 3 Mar 2008 16:02:35 -0600

Thanks for the input, guys!
Your comments are very helpful... I've just opened the door on this whole vector thing, since I am pretty tired of messing with (and cleaning up) dynamic memory allocations. iterators seem like a real pain - fortunately one can just use direct [] access (with vectors at least).
-Dave

----- Original Message ----- From: "Matthew Weigel" <unique@xxxxxxxxxxx>
To: <gameprogrammer@xxxxxxxxxxxxx>
Sent: Monday, March 03, 2008 12:36 AM
Subject: [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




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


Other related posts: