   Arrays vs. Vectors!
  Hello,I just started learning about vectors today, and read that vectors are basically like arrays, but more flexable and powerful. Knowing this, is there any reason to ever use an array instead of a vector, or even use an array again for that matter? If so, what advantages does an array have over a vector?Thanks for any help.   
  Arrays have less overhead.  But it only works if you already know the size of the array.  I only use vectors when I need an array of changing size or unknown at time of coding.theTroll   ;Quote:Original post by TheTrollArrays have less overhead.  But it only works if you already know the size of the array.  I only use vectors when I need an array of changing size or unknown at time of coding.theTrollSorry, but whats "less overhead" mean? [looksaround]I've heard that before, but havent a clue as to what that means.I searched dictionary.com and got,"1. Located, functioning, or originating from above. 2. Of or relating to the operating expenses of a business."But i dont see how it relates to arrays except less functioning? thats my only guess that seems to fit, but im probible wrong. Please help.-- Thank you for any help   ;  Overhead can be either extra memory, or extra processing cycles.  In this case it is a little of both.  Not much in the processing cycles unless you are resizing, inserting in the middle of the array or something like that.  Now if you notice, the array can't do either of those.theTroll   ;  Arrays can also be resized dynamically.Its easier to work with vectors, so you should use them when ever they give you what you need and they're not the bottleneck of your application.Keep in mind that when a vector reaches its maximum size, it reallocates twice as much memory and copies all of its elements to the new place.  This can be a MAJOR overhead if for some reason you want to store instances of objects in the vector (actually, it will store a copy of the object), so you should prefer storing pointed of new'd objects and when ever the object containing the vector is destroyed, delete every items in the vector (you gotta watch out for other objects referencing it, but it would be the same thing with arrays).   ;  @ Troll/Eric -Thanks for the explinations, they helped a lot [smile]   ;Quote:Original post by xEricxArrays can also be resized dynamically.Its easier to work with vectors, so you should use them when ever they give you what you need and they're not the bottleneck of your application.Keep in mind that when a vector reaches its maximum size, it reallocates twice as much memory and copies all of its elements to the new place.  This can be a MAJOR overhead if for some reason you want to store instances of objects in the vector (actually, it will store a copy of the object), so you should prefer storing pointed of new'd objects and when ever the object containing the vector is destroyed, delete every items in the vector (you gotta watch out for other objects referencing it, but it would be the same thing with arrays).Actually you can control how it resizes itself.  By default it may double but you can set it to only go up by 1 element etc.  Heck you can even write your own allocator if you want.Use vectors/lists/etc 100% of the time until a profiler shows you that it's the bottle neck in your application.   ;Quote:Original post by Mike2343Use vectors/lists/etc 100% of the time until a profiler shows you that it's the bottle neck in your application....or you need to allocate off the stack and not off the heap.;Quote:Original post by Mike2343Actually you can control how it resizes itself.  By default it may double but you can set it to only go up by 1 element etc.  Heck you can even write your own allocator if you want.Use vectors/lists/etc 100% of the time until a profiler shows you that it's the bottle neck in your application.How do you control how it resizes itself? You can provide a custom allocator, but the resize policy is hardcoded into the vector itself.Also I wouldn't use them 100 % of the time, it's like saying use templates all the time even though you will always take an integer. An array have the nice property of being of a static size, this means that you might not notice a flaw in your logic with a vector because it will just adapt to your errors. Of course if you need resizing you should initially use a container like a vector or deque. If you suggested vector because of all the stuff like iterators and algorithms, then you should instead look into boost::array, which is an STL-style static size array.   ;  The core difference between Vectors and Arrays are:* Vectors are inheritly heap-based memory storage.* Vectors are wrapped up in nice and pretty class code.* Vectors are effectively a heap-based array wrapped in lots of useful management code.Meanwhile, Arrays:* Can be stack-based memory storage.* Can be manually resized if they are on the heap.* Boost has a nice and pretty class-based wrapper for Arrays.  But most people when they say Array mean the language-based array.Using Vectors as a default container is a decent piece of advice.  You will sometimes want to use other containers, but Vectors are a good default choice.