Skip to main content
gamedev.net
🔒 Locked

array? or vector?

Started by BloodLust666 Dec 8, 2005 at 7:34 PM 15 replies 850+ views
Original Post
BloodLust666
BloodLust666
I need an array of 512 bool values. should i have a and just .reserve(512) or just declare a bool myarray[512];?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
dave
dave
Well it depends on how you are going to use the memory. If you are going to want to move things around alot, make it a stack or queue, something like that, then use vector. But if you just want to store some arbitrary static data then use an array.

Ofcourse the primary feature of a vector is the resizability of it, since you are using a fixed amount it might be cause for an array.

Dave
doynax
doynax
A std::bitset<512> is a space-efficient solution (without the overhead of the ugly boolean specialization of a dynamic vector). It does depend on what you actually want to do with however.
silverphyre673
silverphyre673
Definitely depends on how you're going to use it. Actually, contrary to the beliefs of some, a vector will compile down so that it doesn't take up more space than an array, so you aren't really taking a performace hit if you use it right. It really does depend on how you're using it, though, so more information would be good.
my siteGenius is 1% inspiration and 99% perspiration
BloodLust666
BloodLust666
well, i'm using SDL. and when i Pollevent() i'm going to scroll through each event and (with my 512 bool values) make the key downs true and key ups false.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
doynax
doynax
Quote:
Original post by EvilKnuckles666
well, i'm using SDL. and when i Pollevent() i'm going to scroll through each event and (with my 512 bool values) make the key downs true and key ups false.
Then performance doesn't appear to be an issue, and you don't have to do any serialization. A bitset seems like the cleanest solution.
Of course you might as well use a boolean array or even a vector, it doesn't really matter.
BloodLust666
BloodLust666
can someone post of code about using bitset? i've never used or even heard of that b4. i've always useing and
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
doynax
doynax
class keymap { std::bitset<512> _map;public: void set(unsigned index, bool state) {  _map[index] = state; } bool get(unsigned index) const {  return _map[index]; }};
Oh, and it's initialized to zero by default.
As you can see it's not the hardest container in the world to use.
BloodLust666
BloodLust666
oh wow, that's pretty simple. and bitset is a bool right? b/c i didn't set that being set so i'm assuming bitsets are bools?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
doynax
doynax
Quote:
Original post by EvilKnuckles666
oh wow, that's pretty simple. and bitset is a bool right? b/c i didn't set that being set so i'm assuming bitsets are bools?
Yes.
me22
me22
Just a warning: std::vector is not actually a container.

The standard mandates that it bitpacks the data ( so it fits CHAR_BITS bools in a byte ), but that means that its iterators are non-conforming ( they don't give you bool& ) and that the &v[0] method for getting a pointer to the first element doesn't work ( as an obvious consequence ).
BloodLust666
BloodLust666
alright, now how would I declare a bitset, then change the number of arrays at a later time?

like say i have a class that has a bitset, but the total is unknown and in an Init(long NumArray) function i specify the number i want
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
doynax
doynax
Quote:
Original post by EvilKnuckles666
alright, now how would I declare a bitset, then change the number of arrays at a later time?

like say i have a class that has a bitset, but the total is unknown and in an Init(long NumArray) function i specify the number i want
You don't. It's not a variable length container (the size is built right into the type). If dynamic sizing is an issue then you probably want a vector instead.
BloodLust666
BloodLust666
well that really sucks... well, would it be all that much better to use and .reserve(Num) or have a pointer and new type[Num] it?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Conner McCloud
Conner McCloud
Quote:
Original post by EvilKnuckles666
well that really sucks... well, would it be all that much better to use and .reserve(Num) or have a pointer and new type[Num] it?

I would avoid vector because it behaves unintuitively. If you never resize an object, a pointer array isn't that bad an alternative [although boost::shared_array may be better], but if you do I would use a vector of int/short/char/not bool.

CM
Enigma
Enigma
It would be better not to use a raw allocated array, because then you have to worry about ensuring that it is deallocated exactly once under all execution paths (including execution paths caused by an exception). Reasonable alternatives to std::vector< bool > include boost::shared_array< bool >, boost::scoped_array< bool > and std::deque< bool >. boost::shared_array< bool > should be used if the array is going to be moved around by value or multiple sections of code need to reference the same instance of the array. boost::scoped_array< bool > should be used if the array is not supposed to ever be copied. std::deque< bool > should be used if you need to duplicate the array.

Enigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.