Max array size question

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 06-15-2009
scwizzo
Quote:

Originally Posted by legit
Really? All of the tutorials and books that i've read say that vectors are advanced and powerful tools :S

Doesn't mean they're hard to use or implement. I found it to be a glorious day when I first learned of vectors.

Although, I use both vectors and dynamic arrays interchangeably. Depends on what I'm doing/using it for.
• 06-15-2009
Sebastiani
>> Doesn't mean they're hard to use or implement.

Implementing a full-fledged vector is not as trivial as you may think. Give it a try if you doubt me. ;)
• 06-15-2009
scwizzo
Quote:

Originally Posted by Sebastiani
Implementing a full-fledged vector is not as trivial as you may think. Give it a try if you doubt me. ;)

Never said full-fledged vectors were trivial :)
But something like...

Code:

```//may the code gods have mercy if this is wrong :) #include <iostream> #include <vector> using namespace std; int main() {     vector<int> MyVector;     int i;     for(i = 0; i < 10; i++)         MyVector.push_back(i);     for(i = 0; i < 10; i++)         cout<< MyVector[i] << endl;     return 0; }```
... is simple.
• 06-16-2009
serge
I have a follow up question.

Say, I have the 1G array of doubles. In my application I know for sure that all doubles will always be non-negative.

Now, if I needed another array of the same length containing either 1 or 0, I would have to declare another array of integers. Could I save memory by using the sign-bit (leading bit?) of the double to store the bits? Or would this end in a mess?

SK
• 06-16-2009
anon
It would seem a bit messy.

A smaller datatype for holding ones and zeros would be char. Even smaller should be std::bitset, but then the number of ones and zeros to store should be known at compile time.

If that is unknown the most compact representation would be the std::vector<bool> specialization (but that is not recommended since it works differently from other types of vector).

If you don't mind getting boost, you could also try boost::dynamic_bitset.
• 06-16-2009
serge
Quote:

Originally Posted by anon
It would seem a bit messy.

A smaller datatype for holding ones and zeros would be char. Even smaller should be std::bitset, but then the number of ones and zeros to store should be known at compile time.

If that is unknown the most compact representation would be the std::vector<bool> specialization (but that is not recommended since it works differently from other types of vector).

If you don't mind getting boost, you could also try boost::dynamic_bitset.

Yes, good idea to store such an array as an integer. I am already using something like this. The code would be:

Code:

```// BIT MANIPULATIONS #define BOOL(x) ( !(!(x)) ) #define BIT_TEST( arg , pos ) BOOL( (arg)&(1L << (pos)) ) #define BIT_FLIP( arg , pos ) ( (arg)^(1L << (pos)) ) // suppose you want an array with 1's in the 3rd and 10th positions unsigned int n = 0; n = BIT_FLIP( n , 3 ); n = BIT_FLIP( n , 10 ); // read the digits using BIT_TEST, e.g. for ( i = 10; i--; ) { printf( "%u\n" , BIT_TEST( n , i ) ); }```
EDIT: maybe one has to reverse the order in BIT_TEST
• 06-16-2009
anon
And with bitset, the same code without any macros would be

Code:

```#include <bitset> #include <iostream> int main() {     std::bitset<32> n;     n.flip(3);     n.flip(10);     for ( int i = 11; i--; ) {         std::cout << n.test(i) << '\n';     } }```
The difference is that you are not limited with the size of one unsigned (bitset uses an array of unsigneds for storage).
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12