Home » Community » Forums » General Programming » stl vector vs array
Intel sponsors gamedev.net search:
Control Panel Register Bookmarks Who's Online Active Topics Stats FAQ Search

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 stl vector vs array
Post New Topic  Post Reply 
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]

 User Rating: 1142   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1100   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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]

 User Rating: 1822   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

thx dude, i'll check that book later. so would you suggest that i use vectors for everything rather than standard arrays?

 User Rating: 1142   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 2059   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.




 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by Stoffel
Go to your local library or book store and pick up "Effective STL" by Scott Meyers.
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.

 User Rating: 1139   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by jwalker
you can set the capacity of the vector at the start to increase the speed if you know how many elements are going in.

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:
Original post by civguy
If I have already read Bjarne Stroustrup's "The C++ Programming Language", should I also read that one?

Yes, and probably about 20 others aswell.

 User Rating: 1255   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by SabreMan
Yes, and probably about 20 others aswell.
Come on, you can't be serious... Can you? STL isn't that hard of a subject, at least when it comes down to using it and not studying how it's made.



 User Rating: 1139   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by civguy
Come on, you can't be serious... Can you? STL isn't that hard of a subject, at least when it comes down to using it and not studying how it's made.

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.

 User Rating: 1255   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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 . But it seems like E STL covers some more

 User Rating: 1139   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

The characteristics of a deque also make it worth considering.


 User Rating: 1040   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by civguy

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.

 User Rating: 1100   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

quote:
The characteristics of a deque also make it worth considering.


A bit later than I expected, but still entirely predictable.

 User Rating: 1126   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.


 User Rating: 1040   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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












 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by dede
As the standard goes, vectors should be layed out in memory just like an array, i.e.



Should, yes. But by no means is it required.

 User Rating: 1168   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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> myvector just dies horribly.

 User Rating: 1542   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

quote:
A vector access is atleast that plus a function call.

My compiler would beg to differ.

I assume yours has heard of inline functions? If not, why not?



 User Rating: 1040   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by Deyja
If it doesn't change size, and you do not know how big it will be at compile-time, pick either or.

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:
Original post by Deyja
And it seems MSVC++6 chokes on a vector of vectors. :/ vector<vector<myclass>> myvector just dies horribly.

That's because you did it incorrectly. Try this instead:
  
vector <vector <myclass> > myvector;
  

Notice the space I inserted?

 User Rating: 1015    Report this Post to a Moderator | Link

Err, where I say 'constructor' I meant 'destructor'.

 User Rating: 1015    Report this Post to a Moderator | Link

quote:
Original post by Deyja
The rules for choosing are very simple...

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.

 User Rating: 1255   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by Deyja
Vectors have slower access - no matter how good the implementation is.

This is one of the most common myths regarding STL. And it is just a myth. Its also a veiled form of premature optimisation.

 User Rating: 1255   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: