>> 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.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Never said full-fledged vectors were trivial
But something like...
... is simple.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; }
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
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.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Yes, good idea to store such an array as an integer. I am already using something like this. The code would be:
EDIT: maybe one has to reverse the order in BIT_TESTCode:// 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 ) ); }
Last edited by serge; 06-16-2009 at 04:12 AM.
And with bitset, the same code without any macros would be
The difference is that you are not limited with the size of one unsigned (bitset uses an array of unsigneds for storage).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'; } }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.