   std::vector and other containers
  I continually notice advanced users suggest to beginners that they use vectors as opposed to arrays.  This makes sense.But when I take a look at the rest of the STL, there are a bunch of different containers in there, and I never hear anyone mention their merits.  Why is it that there is such a pervasive preference for the vector class over other containers in the STL, despite the fact that these other containers were also designed to accommodate effectively a variety of different applications?I can't imagine anyone would say that the vector container is flat out better than some of the other containers.  Perhaps it is simply quite versatile?  Or is it just very well suited for game development purposes? Can anyone answer this question?   
  Hey, a self resizing array is a goddamned useful thing at the end of the day. Nearly everything I ever do is covered by vector and occasionally map (or unordered_map). Lists suck for performance, dequeues are rarely a better choice than vectors, stack and queue are just plain annoying, and the other associative containers are just not very widely applicable.   ;  Vectors offer random access and mimic arrays very well.I use lists for things which are added/removed frequently.   ;  People tend to jump to recommending vectors because they're the container most analogous to arrays in the standard library, to the point of actually being one under the hood -- you can literally go some_c_api( &vector.first() );, where some_c_api expects a C style array, and it is guaranteed to work by the standard*.* unless you're using std::vector<bool> :(It's probably generally hoped that these beginners will then go on to discover the other containers in the standard library at their leisure and use them as they see fit.   ;Quote:Original post by PromitLists suck for performanceReally?  Damnit!  I've been using them frequently.  What make's them so bad?   ;  std::vector should be used as a direct replacement of arrays, as it has the same properties. Other containers should be used if there are other requirements on the container.std::list has comlexity O(1) insertion and removal, bur O(n) for finding a particular element.std::set has complexity O(log n) insertion, removal and finding an element, but does not preserve order.std::map is similar to std::set, but used for retrieving objects based on a key value.http://www.gamedev.net/community/forums/post.asp?method=reply&topic_id=545115std::queue for fifo queue usagestd::stack for lifo stack usage....and so on. The point is: use whatever container that will fit the usage requirement. std::vector is definitely not better for than any other container for game development. Its just more common, as its more often what is needed.;Quote:Original post by CrazyfoolQuote:Original post by PromitLists suck for performanceReally?  Damnit!  I've been using them frequently.  What make's them so bad?Well, for one, their allocations are scattered all over memory, which is hard on cache performance for the simplest of tasks -- iterating over a list.  Compounding the issue, there's no random or psuedo random access -- so to get to any given element in a list, you have to iterate at O(N) to find it, which we've already established as being relatively slow for a list.Where std::list wins out is when you NEED a specific order, don't need to frequently iterate the list, can store the list iterators over a long period, ned to insert or remove from into the middle with said iterators, and can avoid getting bitten by iterator invalidation during all this -- but that's an exceedingly rare combination of extenuating circumstances, to be honest.   ;Quote:Original post by MaulingMonkeyyou can literally go some_c_api( &vector.first() );, where some_c_api expects a C style array, and it is guaranteed to work by the standard*.* unless you're using std::vector<bool> :(Of course, C APIs aren't exactly in the habit of expecting bool* so I imagine it doesn't matter all that often :) Although actually I've personally never had a use for std::vector<bool> anyway.Quote:Where std::list wins out is when you NEED a specific order, don't need to frequently iterate the list, can store the list iterators over a long period, ned to insert or remove from into the middle with said iterators, and can avoid getting bitten by iterator invalidation during all this -- but that's an exceedingly rare combination of extenuating circumstances, to be honest.It also wins out where your element type is ridiculously slow to copy, or where it's useful to have them not move around in memory. Oh, and then there's .splice(). :)   ;  hmm.  What exactly is the difference between a stack and a vector.  A vector definitely seems to be organized like a stack, with the pop_back and push-back functions.   But then again it also has erase and insert, giving it some semblance of random access...Do most schools have a class on the Standard library?   ;Quote:Original post by zoner7Do most schools have a class on the Standard library?CS-oriented courses used to teach ADTs. These days, they tend to teach Design Patterns instead.   