|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| stl vector vs array |
|
![]() necromancer_df Member since: 8/11/2002 From: Birmingham, United Kingdom |
||||
|
|
||||
| does anyone know how the access times of an stl vector compare to a n array? i haven't really been able to find any useful comparisons. the best i've been able to come with is that the speed difference is "not noticable". so which do you think is better to store all of my game objects in, an array or a vector, or something totally different? edit: sorry if this comes up a lot, the search page has been down for a few hours [edited by - necromancer_df on September 6, 2002 2:03:42 AM] |
||||
|
||||
![]() Stoffel Member since: 12/2/1999 From: San Diego, USA |
||||
|
|
||||
| Go to your local library or book store and pick up "Effective STL" by Scott Meyers. He lays down in the first tip or two when to use each container. Vectors are just as fast as dynamic arrays (in fact, they are dynamic arrays), so that should answer your question. |
||||
|
||||
![]() Zipster GDNet+ Member since: 3/11/2000 From: Las Vegas, NV, United States |
||||
|
|
||||
| I like to think of them as "sophisticated arrays"... like Stoffel said, they can be dynamic, plus you can perform special operations such as random insertions and deletions, granted you get the speed hits, since this still behaves like an array mind you. But it's a lot easier than implementing your own dynamic array. Three cheers for library code written by someone else! [edited by - Zipster on September 6, 2002 2:19:28 AM] |
||||
|
||||
![]() necromancer_df Member since: 8/11/2002 From: Birmingham, United Kingdom |
||||
|
|
||||
| thx dude, i'll check that book later. so would you suggest that i use vectors for everything rather than standard arrays? |
||||
|
||||
![]() Sneftel Moderator - Consoles, PDAs, and Cell Phones Member since: 7/7/2001 From: Philadelphia, PA, United States |
||||
|
|
||||
| When in doubt, use a vector. There are certain cases where arrays are better, tho. The main problem with vectors is that there's a certain amount of time taken to create/destroy them. Static arrays, however, are allocated on the stack, and take pretty much no time to allocate. So if you had a function, say, that used as temporary storage an array of 5 integers, you'd be better off using an array than a vector. Don't listen to me. I've had too much coffee. |
||||
|
||||
![]() jwalker Member since: 2/8/2000 From: Petaling Jaya, Malaysia |
||||
|
|
||||
| you can set the capacity of the vector at the start to increase the speed if you know how many elements are going in. and memory in a vector is linear, so you can always do this abc.begin() + 5 to access the 6th element. |
||||
|
||||
![]() civguy Member since: 10/23/2000 From: Zimbabwe |
||||
|
|
||||
quote:If I have already read Bjarne Stroustrup's "The C++ Programming Language", should I also read that one? Stroustrup's book had a fair share of STL too. |
||||
|
||||
![]() SabreMan Member since: 2/12/2002 |
||||
|
|
||||
quote: The same concerns apply to arrays. Basically, the C++ concept of an array is C legacy - something that "they" thought was a good idea in 1976. The problems with arrays are well understood, so a vector should be regarded as the 21st century idea of what an array should be. To put a rule of thumb on it: always prefer vectors to arrays unless you have a proven specific need for an array. For many (probably most) applications, you should not use arrays at all. quote: Yes, and probably about 20 others aswell. |
||||
|
||||
![]() civguy Member since: 10/23/2000 From: Zimbabwe |
||||
|
|
||||
quote:Come on, you can't be serious... Can you? |
||||
|
||||
![]() SabreMan Member since: 2/12/2002 |
||||
|
|
||||
quote: Semi-serious. The point I'm really making is that there's always more books to read, and more to learn about programming, whether it be C++, STL, or any other topic. Reading and learning should continue throughout a s/w developers career, so when you ask 'If I have already read Bjarne Stroustrup's "The C++ Programming Language", should I also read that one?', the answer is, of course, yes! On a slightly more serious note, Effective STL will give you an insight into how to put the STL to good use, and to avoid various pitfalls of the library. Many of the tips and tricks are not explained in other texts. If you're a keen reader, its well worth a look. |
||||
|
||||
![]() civguy Member since: 10/23/2000 From: Zimbabwe |
||||
|
|
||||
| Ok, I think I'll try to obtain it (gotta check library first). What I really wanted to know with my original question was if Effective STL only covered what's taught in Bjarne's book. For example, it wouldn't be of any use for me to read a book called "C++ basics" now |
||||
|
||||
![]() DrPizza Member since: 12/26/2001 From: Airstrip One |
||||
|
|
||||
| The characteristics of a deque also make it worth considering. |
||||
|
||||
![]() Stoffel Member since: 12/2/1999 From: San Diego, USA |
||||
|
|
||||
quote:If I have already read Bjarne Stroustrup's "The C++ Programming Language", should I also read that one? Stroustrup's book had a fair share of STL too. Absolutely. Scott's book is basically: "So you've been using STL--here's what you're probably doing wrong and how to do it the right way." Meyers' books on C++ and STL are some of the best intermediate programming books I've read. |
||||
|
||||
![]() null_pointer Member since: 11/10/1999 From: USA |
||||
|
|
||||
quote: A bit later than I expected, but still entirely predictable. |
||||
|
||||
![]() DrPizza Member since: 12/26/2001 From: Airstrip One |
||||
|
|
||||
| Meyers' book is not a beginners' guide, and is much better for it. It gave me a number of "doh!" moments. Which I *think* is a good thing. |
||||
|
||||
![]() dede Member since: 8/10/2002 From: Michigan, USA |
||||
|
|
||||
| Remember, STL is the Standard that is not the Standard. Really. All STL implementation do it alittle differently, so I doubt you will find an example that shows the correct costs, etc...,Since it will vary depending on which compiler and which processor you have... As the standard goes, vectors should be layed out in memory just like an array, i.e. array + 5 -> array[5] vector + 5 -> vector[5] So the access time will be the same... And if its not like that, it should create an exception and reorginize itself so the next access is.... at least according to the standard |
||||
|
||||
![]() daerid Member since: 8/13/2001 From: Sunland, CA, United States |
||||
|
|
||||
quote: Should, yes. But by no means is it required. |
||||
|
||||
![]() Deyja Member since: 3/5/2002 From: Stafford, VA, United States |
||||
|
|
||||
| The rules for choosing are very simple... If it changes size, use a vector. If it doesn't change size, and you know how big it will be at compile-time, use an array. If it doesn't change size, and you do not know how big it will be at compile-time, pick either or. Vectors have slower access - no matter how good the implementation is. An array access is an addition and a pointer de-reference. A vector access is atleast that plus a function call. And it seems MSVC++6 chokes on a vector of vectors. :/ vector |
||||
|
||||
![]() DrPizza Member since: 12/26/2001 From: Airstrip One |
||||
|
|
||||
quote: My compiler would beg to differ. I assume yours has heard of inline functions? If not, why not? |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
quote: If a relative newbie was using exceptions (or using something that used exceptions, et cetera) I'd tell them never to have dynamically allocated memory that wasn't freed in a constructor (because it could be 'thrown over' and never freed), so vectors could be considered preferable in that situation too. I'll admit that I don't use vectors too often (although I do use a hand-made 'auto pointer' style class quite a lot, which handles the 'thrown over' issue for me). quote: That's because you did it incorrectly. Try this instead: vector <vector <myclass> > myvector; Notice the space I inserted? |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| Err, where I say 'constructor' I meant 'destructor'. |
||||
|
||||
![]() SabreMan Member since: 2/12/2002 |
||||
|
|
||||
quote: I prefer a simpler rule: unless you specifically need an array, use a vector. The avoidance of arrays can be justified on the grounds of general array bogosity, such as passing to a function, finding out the size, and returning from a function. |
||||
|
||||
![]() SabreMan Member since: 2/12/2002 |
||||
|
|
||||
quote: This is one of the most common myths regarding STL. And it is just a myth. Its also a veiled form of premature optimisation. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|