🔒 Locked
array? or vector?
Advertisement
Original Post
I need an array of 512 bool values. should i have a and just .reserve(512) or just declare a bool myarray[512];?
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
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
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.
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.
Quote: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.
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.
Of course you might as well use a boolean array or even a vector, it doesn't really matter.
can someone post of code about using bitset? i've never used or even heard of that b4. i've always useing and
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?
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 ).
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 ).
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
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
Quote: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
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
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?
Quote:
Original post by EvilKnuckles666
well that really sucks... well, would it be all that much better to useand .reserve(Num) or have a pointer and new type[Num] it?
I would avoid vector
CM
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
Enigma
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement